生成缺少日期的日期范围并分配当前最大值

时间:2018-07-01 02:55:22

标签: sql date db2

我在DB2中有一个如下表-

 Date       |       Catg|      Amount
 2018-05-21 |         2 |      583227.57485
 2018-05-21 |         5 |     2200097.73226
 2018-05-22 |         2 |      116246.63551
 2018-05-22 |         4 |      231116.66241
 2018-05-22 |         5 |      244093.91680
 2018-05-31 |         1 |      244714.77015
 2018-05-31 |         2 |      288946.64734
 2018-05-31 |         3 |      330801.32189
 2018-05-31 |         5 |      345984.62256
 2018-06-05 |         4 |      228612.55653
 2018-06-05 |         5 |      244944.22519
 2018-06-11 |         2 |      288940.63303
 2018-06-11 |         3 |      344938.50723
 2018-06-11 |         4 |      346234.65196
 2018-06-11 |         5 |      375935.22568

我想为每个catt生成6月至22日的报告。所以我希望报告为-

 Date       |       Catg|      Amount

 2018-06-01 |         1 |      244714.77015     -- Being 5/31 is latest for 6/1
 2018-06-01 |         2 |      288946.64734     -- Being 5/31 is latest for 6/1
 2018-06-01 |         3 |      330801.32189     -- Being 5/31 is latest for 6/1
 2018-06-01 |         4 |      231116.66241     -- Being 5/22 is latest for 6/1     
 2018-06-01 |         5 |      345984.62256     -- Being 5/31 is latest for 6/1
 .
 .
 .
 .
 .
 2018-06-22 |         1 |      244714.77015     -- Being 5/31 is latest for 6/22
 2018-06-22 |         2 |      288940.63303     -- Being 6/11 is latest for 6/22
 2018-06-22 |         3 |      344938.50723     -- Being 6/11 is latest for 6/22
 2018-06-22 |         4 |      346234.65196     -- Being 6/11 is latest for 6/22
 2018-06-22 |         5 |      375935.22568     -- Being 6/11 is latest for 6/22

我不知道这对SQL是否可行。我已经成功生成了日期,但是不确定如何为它们分配前一个立即值。

我已经通过以下代码生成了日期-

WITH DATE_TAB(DATES) AS (                
SELECT DATE('2018-06-01') DATES     
FROM SYSIBM.SYSDUMMY1               
UNION ALL                           
SELECT DATES + 1 DAYS AS DATES      
FROM DATE_TAB                       
WHERE DATES < '2018-06-22')         
SELECT DATES                            
FROM DATE_TAB                           

非常感谢您的帮助。

提前谢谢!

2 个答案:

答案 0 :(得分:1)

其余部分为CROSS JOIN

WITH DATE_TAB(DATES) AS (                
     SELECT DATE('2018-06-01') DATES     
     FROM SYSIBM.SYSDUMMY1               
     UNION ALL                           
     SELECT DATES + 1 DAYS AS DATES      
     FROM DATE_TAB                       
     WHERE DATES < '2018-06-22'
)         
SELECT DISTINCT dt.DATES, dt1.Catg, 
       (SELECT t.Amount
        FROM table t
        WHERE t.catg = dt.catg and t.date <= dt.date
        ORDER BY t.date desc
        FETCH FIRST 1 ROW ONLY
       )                            
FROM DATE_TAB dt 
     CROSS JOIN (SELECT DISTINCT Catg, Amount FROM table) dt1;

答案 1 :(得分:0)

您可以这样做:

with date_tab ( . . . )
select d.dte, c.catg, 
       (select t.amount
        from t
        where t.catg = c.catg and t.dte <= d.dte
        order by t.dte desc
        fetch first 1 row only
       ) as amount
from date_tab d cross join
     (select distinct catg from t) c
order by d.dte, c.catg;