使用 Oracle11g:
如果该列为空,我想排除该列上的算术。
在这种情况下,我怎样才能让seqno=1的lag_diff为null?
with detail_records as (
select 1 seqno, 10 set_a from dual union all
select 2 seqno, 10 set_a from dual union all
select 3 seqno, 20 set_a from dual union all
select 4 seqno, 30 set_a from dual )
select seqno,
set_a,
lag_val,
case (lag_val - set_a)
when 0 then 'AAA'
else 'BBB'
end as lag_diff
from ( select seqno,
set_a,
lag(set_a,1) over (order by seqno) as lag_val
from detail_records)
order by seqno
期望的结果
SEQNO SET_A LAG_VAL LAG_DIFF
----------------------------
1 10 <NULL> <null>
2 10 10 AAA
3 20 10 BBB
4 30 20 BBB