假设我有A列,并且当前获取的A值为null。我需要返回到上一行,并找到列A的非null值。然后我需要从看到非值的点到当前点,找到另一列B的总和。之后,我需要将B与A相加,这将是A的新值。
要查找列A非空值,我已将查询写为
nvl(last_value(nullif(A,0)) ignore nulls over (order by A),0)
但是我需要如上所述进行B的计算。
nvl(last_value(nullif(A,0)) ignore nulls over (order by A),0)
有人可以帮我吗?
样本数据
A B date
null 20 14/06/2019
null 40 13/06/2019
10 50 12/06/2019
此处应将A的14/06/2019的值替换为B + A的12/06/2019的值(这是A的第一个非空值)= 20 + 40 + 50 + 10 = 120
答案 0 :(得分:0)
我认为您要使用
处理 sum(<column>) over (...)
和last_value over (...)
的功能如下
:
with t( A,B, "date" ) as
(
select null, 20, date'2019-06-14' from dual union all
select null, 40, date'2019-06-13' from dual union all
select 10 ,50, date'2019-06-12' from dual
)
select nvl(a,sum(b) over (order by 1)+
last_value(a) ignore nulls
over (order by 1 desc)
) as a,
b, "date"
from t;
A B date
--- -- ----------
120 20 14.06.2019
120 40 13.06.2019
10 50 12.06.2019
答案 1 :(得分:0)
如果您具有12c或更高版本:
with t( A,B, dte ) as
(
select null, 20, date'2019-06-14' from dual union all
select null, 40, date'2019-06-13' from dual union all
select 10 ,50, date'2019-06-12' from dual
)
select * from t
match_recognize(
order by dte desc
measures
nvl(
first(a),
y.a + sum(b)
) as a,
first(b) as b,
first(dte) as dte
after match skip to next row
pattern(x* y{0,1})
define x as a is null,
y as a is not null
);
A B DTE
------ ---------- ----------
120 20 2019-14-06
100 40 2019-13-06
10 50 2019-12-06
答案 2 :(得分:0)
使用条件计数将数据分为不同的组,然后使用该组进行分析计算:
select a, b, dt, grp, sum(nvl(a, 0) + nvl(b, 0)) over (partition by grp order by dt) val
from (
select a, b, dt, count(case when a is not null then 1 end) over (order by dt) grp
from t order by dt desc)
order by dt desc
抽样结果:
A B DT GRP VAL
------ ---------- ----------- ---------- ----------
20 2019-06-14 4 120
40 2019-06-13 4 100
10 50 2019-06-12 4 60
5 2 2019-06-11 3 7
6 1 2019-06-10 2 7
3 2019-06-09 1 14
7 4 2019-06-08 1 11