Oracle 分析函数,数据来自最后一行,但 1 行



我看到,在我们的一个表中,具有最新时间戳的行在大多数列中都有 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
您可以使用

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;

演示

相关内容

  • 没有找到相关文章

最新更新