[英]how to assign a rank for null values with previous first non-null value in oracle
I need to assign a rank to some null values over ordered rows. 我需要为有序行上的某些空值分配等级。
My query is like this : 我的查询是这样的:
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
and result set is like this : 结果集是这样的:
id inx num
----------
10 1 2
10 2 null
10 3 8
10 4 null
i'm tring to set null values with previous first non-null value 我正在尝试使用先前的第一个非空值设置空值
for example : num null value should be "2" where inx = 2 例如:num null值应为“ 2”,其中inx = 2
and num null value should be "8" where inx = 4 and so on. 并且num null值应为“ 8”,其中inx = 4,依此类推。
thx for any idea.. 谢谢任何想法..
If you know that the values are increasing, you can just use max()
: 如果您知道值在增加,则可以使用
max()
:
select id, inx, max(num) over (partition by id order by inx) as num
If they are not increasing and multiple nulls never appear in a sequence, you can use lag()
: 如果它们没有增加并且序列中永远不会出现多个null,则可以使用
lag()
:
select id, inx,
(case when num is null
then lag(num) over (partition by id order by inx)
else num
end)as null;
If nulls do appear in a sequence, you can use the ignore nulls
option to lag()
: 如果空值确实出现在序列中,则可以对
lag()
使用“ ignore nulls
选项:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.