我需要在有序行上为某些空值分配一个等级 我的查询是这样的:
with sub as
(
select 10 as id, 1 as inx,2 as num from dual
union all
select 10 as id, 2 as inx,null as num from dual
union all
select 10 as id, 3 as inx,8 as num from dual
union all
select 10 as id, 4 as inx,null as num from dual
)
select *
from sub order by inx
,结果集如下:
id inx num
----------
10 1 2
10 2 null
10 3 8
10 4 null
我想用先前的第一个非空值设置空值
例如:num null值应为“2”,其中inx = 2
和num null值应为“8”,其中inx = 4,依此类推。
thx任何想法..
答案 0 :(得分:1)
如果您知道值正在增加,则可以使用max()
:
select id, inx, max(num) over (partition by id order by inx) as num
如果它们没有增加且多个空值永远不会出现在序列中,则可以使用lag()
:
select id, inx,
(case when num is null
then lag(num) over (partition by id order by inx)
else num
end)as null;
如果序列中出现空值,则可以使用ignore nulls
选项lag()
:
select id, inx,
(case when num is null
then lag(num ignore nulls) over (partition by id order by inx)
else num
end)as null