如何利用SQL语句实现?
有如下表结构,时间戳 汇率1汇率2时间戳不重复,要求补充红色部分字体的数据,红色字体的数据与上一次不为空的数据相同,需要用Over语句实现?
2008-03-14-22.00.01.572000 99.86 1.0018
2008-03-14-22.00.03.581000 99.86 1.0015
2008-03-14-22.00.05.584000 99.84 1.0015
2008-03-14-22.00.07.584000 99.85 1.0015
2008-03-14-22.00.09.980000 99.85 1.0015
2008-03-14-22.00.11.916000 99.85 1.0015
2008-03-14-22.00.13.912000 99.85 1.0014
2008-03-14-22.00.15.921000 99.85 1.0014
2008-03-14-22.00.17.927000 99.85 1.0014
2008-03-14-22.00.19.927000 99.85 1.0014
2008-03-14-22.00.21.928000 99.85 1.0014
2008-03-14-22.00.23.928000 99.85 1.0014
2008-03-14-22.00.25.931000 99.85 1.0009
2008-03-14-22.00.27.931000 99.69 1.0006 回复 1# zhaoqinghu
select timestamp,(case when r1 is null then LAG(r1,1,0,'IGNORE NULLS') over(order by timestamp) else r1 end) as rate1,(case when r2 is null then LAG(r2,1,0,'IGNORE NULLS') over(order by timestamp) else r2 end) as rate2 from yourtable; 回复 2# ywzj
我使用的是Db2 8.2版本,没有LEAD,LAG,FIRST_VALUE,LAST_VALUE等,请问如何变通实现,谢谢! 回复 3# zhaoqinghu
把LAG换成自关联的子查询。 回复 4# ywzj
用子查询相当于不使用OVER语句了是吗?
如下:
db2 "select a.dt,case when a.r1 is null then (select b.r1 fromtemp as b where b.dt<=a.dt and b.r1 is not null order by b.dt desc fetch first 1 rows only) else a.r1 end from temp as a" 回复 5# zhaoqinghu
使用子查询(利用case when语句),效率相当低,请问有没高手指点?使用的db2属于8.2版本
页:
[1]