我看到,在我们的一个表中,具有最新时间戳的行在大多数列中都为NULL,但最后一行具有正确的值。有没有办法将那些值从最后一行转移到最后一行?
我一直在尝试找到结合CASE逻辑/分析功能的解决方案,但无法解决。任何想法都值得赞赏。
当前输出:
with t as
(
select '99WC2003' as pol_id, to_date( '1/23/2017','mm/dd/yyyy') as Eff_dt , NULL as industry , '-1' as Model from dual Union all
select '99WC2003' as pol_id, to_date('1/22/2017','mm/dd/yyyy') as Eff_dt , 'retail' as industry , '0.34' as Model from dual Union all
select '99WC2003' as pol_id, to_date('1/21/2017','mm/dd/yyyy') as Eff_dt , 'retail' as industry , '0.55' as Model from dual Union all
select '10XYZ02' as pol_id, to_date('1/12/2019','mm/dd/yyyy') as Eff_dt , NULL as industry , '0' as Model from dual Union all
select '10XYZ02' as pol_id, to_date('1/11/2019','mm/dd/yyyy') as Eff_dt , 'tech' as industry , '0.3' as Model from dual
)
select pol_id, Eff_dt, industry, model from t
预期输出:
with t as
(
select '99WC2003' as pol_id, to_date('1/23/2017','mm/dd/yyyy') as Eff_dt , 'retail' as industry , '0.34' as Model from dual Union all
select '99WC2003' as pol_id, to_date('1/22/2017','mm/dd/yyyy') as Eff_dt , 'retail' as industry , '0.34' as Model from dual Union all
select '99WC2003' as pol_id, to_date('1/21/2017','mm/dd/yyyy') as Eff_dt , 'retail' as industry , '0.55' as Model from dual Union all
select '10XYZ02' as pol_id, to_date('1/12/2019','mm/dd/yyyy') as Eff_dt , 'tech' as industry , '0.3' as Model from dual Union all
select '10XYZ02' as pol_id, to_date('1/11/2019','mm/dd/yyyy') as Eff_dt ,'tech' as industry , '0.3' as Model from dual
)
select pol_id, Eff_dt, industry, model from t
答案 0 :(得分:1)
您可以使用lead()
分析函数:
select pol_id, Eff_dt,
nvl(industry,
lead(industry) over (order by pol_id desc,Eff_dt desc)) as industry,
case when nvl(model,0)<=0 then
lead(model) over (order by pol_id desc,Eff_dt desc)
else model end as model
from t;