2017-02-28 76 views
1

我一直在嘗試一個連接,起初我認爲這個連接相對簡單,但現在遇到了一些麻煩,使它完全正確。我有兩套這類似於下面的數據用最後一個非null連接並填充NULL值

ID | stmt_dt    ID | renewal_dt 
    --      -- 
1 |1/31/15    1 | 2/28/15 
1 |2/28/15    1 | 4/30/15 
1 |3/31/15    2 | 2/28/15 
1 |4/30/15    3 | 1/31/15 
1 |5/31/15     
2 |1/31/15 
2 |2/28/15 
2 |3/31/15 
2 |4/30/15 
2 |5/31/15 
3 |1/31/15 
3 |2/28/15 
3 |3/31/15 
3 |4/30/15 
3 |5/31/15 
4 |1/31/15 
4 |2/28/15 
4 |3/31/15 
4 |4/30/15 
4 |5/31/15 

這是我想要的輸出

ID | stmt_dt | renewal_dt 
    --   
1 |1/31/15 | NA 
1 |2/28/15 | 2/28/15   
1 |3/31/15 | 2/28/15   
1 |4/30/15 | 4/30/15   
1 |5/31/15 | 4/30/15   
2 |1/31/15 | NA 
2 |2/28/15 | 2/28/15 
2 |3/31/15 | 2/28/15 
2 |4/30/15 | 2/28/15 
2 |5/31/15 | 2/28/15 
3 |1/31/15 | 1/31/15 
3 |2/28/15 | 1/31/15 
3 |3/31/15 | 1/31/15 
3 |4/30/15 | 1/31/15 
3 |5/31/15 | 1/31/15 
4 |1/31/15 | NA 
4 |2/28/15 | NA 
4 |3/31/15 | NA 
4 |4/30/15 | NA 
4 |5/31/15 | NA 

我最大的問題已經越來越合併後的值來填充到每個組內的下一個非空。關於如何實現這種聯接的任何想法?謝謝!

+1

嘗試更好的解釋。 –

+1

嘗試'LAST_VALUE(renewal_dt IGNORE NULLS)OVER(PARTITION BY ID ORDER BY STMT_DT)' – dnoeth

+0

@dnoeth釘了它。謝謝! –

回答

0

UNION ALL + LAST_VALUE

select  ID 
      ,dt as stmt_dt 

      ,last_value (case when tab = 'R' then dt end ignore nulls) over 
      (
       partition by id 
       order by  dt 
           ,case tab when 'R' then 1 else 2 end 
      ) as renewal_dt 

from  (   select 'S',ID,stmt_dt from stmt 
      union all select 'R',ID,renewal_dt from renewal 
      ) as t (tab,ID,dt) 


qualify  tab = 'S' 

order by ID 
      ,stmt_dt 

+----+------------+------------+ 
| ID | stmt_dt | renewal_dt | 
+----+------------+------------+ 
| 1 | 2015-01-31 |   | 
| 1 | 2015-02-28 | 2015-02-28 | 
| 1 | 2015-03-31 | 2015-02-28 | 
| 1 | 2015-04-30 | 2015-04-30 | 
| 1 | 2015-05-31 | 2015-04-30 | 
| 2 | 2015-01-31 |   | 
| 2 | 2015-02-28 | 2015-02-28 | 
| 2 | 2015-03-31 | 2015-02-28 | 
| 2 | 2015-04-30 | 2015-02-28 | 
| 2 | 2015-05-31 | 2015-02-28 | 
| 3 | 2015-01-31 | 2015-01-31 | 
| 3 | 2015-02-28 | 2015-01-31 | 
| 3 | 2015-03-31 | 2015-01-31 | 
| 3 | 2015-04-30 | 2015-01-31 | 
| 3 | 2015-05-31 | 2015-01-31 | 
| 4 | 2015-01-31 |   | 
| 4 | 2015-02-28 |   | 
| 4 | 2015-03-31 |   | 
| 4 | 2015-04-30 |   | 
| 4 | 2015-05-31 |   | 
+----+------------+------------+ 
0

分鐘(...)在(1以下1以下之間...行)* +加入
* = LEAD

select s.ID 
     ,s.stmt_dt 
     ,r.renewal_dt 

from    stmt s 

     left join (select ID 
          ,renewal_dt        

          ,min (renewal_dt) over 
          (
           partition by ID 
           order by  renewal_dt 
           rows   between 1 following 
               and  1 following 
          ) as next_renewal_dt 

        from renewal 
        ) r 

     on   s.ID  = r.ID 

       and s.stmt_dt >= r.renewal_dt 
       and s.stmt_dt < coalesce (r.next_renewal_dt,date '9999-01-01') 

order by s.ID 
      ,s.stmt_dt 

+----+------------+------------+ 
| ID | stmt_dt | renewal_dt | 
+----+------------+------------+ 
| 1 | 2015-01-31 |   | 
| 1 | 2015-02-28 | 2015-02-28 | 
| 1 | 2015-03-31 | 2015-02-28 | 
| 1 | 2015-04-30 | 2015-04-30 | 
| 1 | 2015-05-31 | 2015-04-30 | 
| 2 | 2015-01-31 |   | 
| 2 | 2015-02-28 | 2015-02-28 | 
| 2 | 2015-03-31 | 2015-02-28 | 
| 2 | 2015-04-30 | 2015-02-28 | 
| 2 | 2015-05-31 | 2015-02-28 | 
| 3 | 2015-01-31 | 2015-01-31 | 
| 3 | 2015-02-28 | 2015-01-31 | 
| 3 | 2015-03-31 | 2015-01-31 | 
| 3 | 2015-04-30 | 2015-01-31 | 
| 3 | 2015-05-31 | 2015-01-31 | 
| 4 | 2015-01-31 |   | 
| 4 | 2015-02-28 |   | 
| 4 | 2015-03-31 |   | 
| 4 | 2015-04-30 |   | 
| 4 | 2015-05-31 |   | 
+----+------------+------------+ 
0

SELECT查詢相關

select  s.ID 
      ,s.stmt_dt 

      ,( 
       select  max (r.renewal_dt) 
       from  renewal r 
       where  r.ID = s.ID 
         and r.renewal_dt <= s.stmt_dt 
      ) as renewal_dt 

from  stmt s 

order by ID 
      ,stmt_dt 

+----+------------+------------+ 
| ID | stmt_dt | renewal_dt | 
+----+------------+------------+ 
| 1 | 2015-01-31 |   | 
| 1 | 2015-02-28 | 2015-02-28 | 
| 1 | 2015-03-31 | 2015-02-28 | 
| 1 | 2015-04-30 | 2015-04-30 | 
| 1 | 2015-05-31 | 2015-04-30 | 
| 2 | 2015-01-31 |   | 
| 2 | 2015-02-28 | 2015-02-28 | 
| 2 | 2015-03-31 | 2015-02-28 | 
| 2 | 2015-04-30 | 2015-02-28 | 
| 2 | 2015-05-31 | 2015-02-28 | 
| 3 | 2015-01-31 | 2015-01-31 | 
| 3 | 2015-02-28 | 2015-01-31 | 
| 3 | 2015-03-31 | 2015-01-31 | 
| 3 | 2015-04-30 | 2015-01-31 | 
| 3 | 2015-05-31 | 2015-01-31 | 
| 4 | 2015-01-31 |   | 
| 4 | 2015-02-28 |   | 
| 4 | 2015-03-31 |   | 
| 4 | 2015-04-30 |   | 
| 4 | 2015-05-31 |   | 
+----+------------+------------+