Аналитическая функция Oracle, данные из последней, но 1 строки - PullRequest
2 голосов
/ 10 июля 2019

Я вижу, что в одной из наших таблиц строка с последней отметкой времени имеет значения 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

1 Ответ

1 голос
/ 10 июля 2019

Вы можете использовать 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;

Демо

...