为缺失日期生成日期范围并分配当前最大值
Posted
技术标签:
【中文标题】为缺失日期生成日期范围并分配当前最大值【英文标题】:Generate date range for missing dates and assign the current maximum value 【发布时间】:2018-07-01 02:55:21 【问题描述】:我在 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
我想为每个 catg 生成 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
非常感谢任何帮助。
提前致谢!!!
【问题讨论】:
显示您是如何生成日期的。 @GordonLinoff,问题已更新,这些金额是通过此生成的 - ***.com/questions/51116723/… 【参考方案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;
【讨论】:
我认为在内部查询中您的意思是 dt1.catg。WHERE t.catg = dt.catg and t.date <= dt.date
【参考方案2】:
你可以这样做:
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;
【讨论】:
你一定来自另一个星球。以上是关于为缺失日期生成日期范围并分配当前最大值的主要内容,如果未能解决你的问题,请参考以下文章
Python Pandas 插值:在缺失的日期范围内重新分配值