在特定的最后12个月内缺少特定周期时,如何动态添加与最后12个月数据值的行动行?
这是输入表:
项目 | QTY | DATEPERIOD |
---|---|---|
A | 2 | 1/1/2020 0:00 |
A | 3 | 2/1/2020 0:00 |
A | 4 | 3/1/2020 0:00 |
A | 1 | 4/1/2020 0:00 |
A | 2 | 5/1/2020 0:00 |
A | 2 | 6/1/2020 0:00 |
A | 2 | 8/1/2020 |
0:00 A | 2 | 10/1/2020 |
0:00 A | 2 | 12/1/2020 0:00 |
A | 2 | 1/1/2021 0:00 |
A | 3 | 2/1/2021 0:00 |
A | 4 | 3/1/2021 0:00 |
A | 2 | 5/1/2021 |
0:00 A | 2 | 6/1/2021 0:00 |
A | 2 | 8/1/2021 0:00 |
A | 1 | 9/1/2021 0:00 |
A | 2 | 10/1/2021 0:00 |
A | 1 | 11/1/2021 |
A | 1 | 12/1/2021 0:00 |
0:00 输入表没有2021年7月的数据,当我必须计算每个行的最后12个月的数据时,我将能够从2021年12月至2021年8月获得最后12个月的数据。
但是,由于输入表确实确实没有2021年7月的数据,使用通常的查询
SUM(qty) OVER (
PARTITION BY item
ORDER BY dateperiod
RANGE BETWEEN INTERVAL '11' MONTH PRECEDING
AND INTERVAL '0' MONTH FOLLOWING
) AS total,
将生成2021年6月的最后12个月数据。但是,即使数据在7月至2021年不可用,预期的输出也可以在过去12个月内动态生成一行2021年7月至2021年7月至2020年8月的数据。QTY的结果是:19
类似地,输入表丢失了2021年4月的数据。然后,查询与上一个12个月的数据一起生成了一行, 结果应
为
至 | 月 | 。 |
---|---|---|
该数量的 | 年 | 2020 |
2021 | 年 | 4 |
5 | 月 2020 | 9 |
A | 4/1/2020 | 10 |
A | 5/1/2020 | 12 |
A | 6/1/2020 | 14 |
A | 7/1/2020 | 14 |
A | 8/1/2020 | 16 |
A | 9/1/2020 | 16 |
A | 10/1/2020 | 18 |
A | 11/1/2020 | 18 |
A | 12/1/2020 | 20 |
A | 1/1/2021 | 20 |
A | 2/1/2021 | 20 |
A | 3/1/2021 | 20 |
A | 4/1/2021 | 19 |
A | 5/1/2021 | 19 |
A | 6/1/2021 | 19 |
A | 7/1/2021 | 19 |
A | 8/1/2021 | 19 |
A | 9/1/2021 | 20 |
A | 10/1/2021 | 20 |
A | 11/1/2021 | 21 |
A | 12/1/2021/2021 | 20 |
请让我知道这是否可能
This is the input Table:
ITEM | QTY | DATEPERIOD |
---|---|---|
A | 2 | 1/1/2020 0:00 |
A | 3 | 2/1/2020 0:00 |
A | 4 | 3/1/2020 0:00 |
A | 1 | 4/1/2020 0:00 |
A | 2 | 5/1/2020 0:00 |
A | 2 | 6/1/2020 0:00 |
A | 2 | 8/1/2020 0:00 |
A | 2 | 10/1/2020 0:00 |
A | 2 | 12/1/2020 0:00 |
A | 2 | 1/1/2021 0:00 |
A | 3 | 2/1/2021 0:00 |
A | 4 | 3/1/2021 0:00 |
A | 2 | 5/1/2021 0:00 |
A | 2 | 6/1/2021 0:00 |
A | 2 | 8/1/2021 0:00 |
A | 1 | 9/1/2021 0:00 |
A | 2 | 10/1/2021 0:00 |
A | 1 | 11/1/2021 0:00 |
A | 1 | 12/1/2021 0:00 |
This input table does not have data of 2021-July, when I have to calculate the data of last 12 month for each rows, I will be able to get data of last 12 months from dec 2021 to Aug 2021.
But since the input table does not have data of 2021-July, using usual query
SUM(qty) OVER (
PARTITION BY item
ORDER BY dateperiod
RANGE BETWEEN INTERVAL '11' MONTH PRECEDING
AND INTERVAL '0' MONTH FOLLOWING
) AS total,
would generate last 12 month data for June 2021. But the expected output is even if the data is not available in July-2021, is it possible to dynamically generate a row as last 12 month data for July-2021 which should be from July 2021 to Aug 2020. The result of the qty is: 19
similarly, the input table is missing data for April 2021. Then the query generate a row as last 12 month data for April-2021 which should be from April 2021 to May 2020. The result of the qty is: 19
So the expected output will be in the form of
ITEM | DATEPERIOD | Output |
---|---|---|
A | 1/1/2020 | 2 |
A | 2/1/2020 | 5 |
A | 3/1/2020 | 9 |
A | 4/1/2020 | 10 |
A | 5/1/2020 | 12 |
A | 6/1/2020 | 14 |
A | 7/1/2020 | 14 |
A | 8/1/2020 | 16 |
A | 9/1/2020 | 16 |
A | 10/1/2020 | 18 |
A | 11/1/2020 | 18 |
A | 12/1/2020 | 20 |
A | 1/1/2021 | 20 |
A | 2/1/2021 | 20 |
A | 3/1/2021 | 20 |
A | 4/1/2021 | 19 |
A | 5/1/2021 | 19 |
A | 6/1/2021 | 19 |
A | 7/1/2021 | 19 |
A | 8/1/2021 | 19 |
A | 9/1/2021 | 20 |
A | 10/1/2021 | 20 |
A | 11/1/2021 | 21 |
A | 12/1/2021 | 20 |
Please let me know if this is possible
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用层次查询来生成日历,并使用
ofter Join
将其连接到数据(但是,由于您是为每个item
进行操作,然后您可能可能想要一个分区
ed外部加入
):对于您的示例数据:
输出:
db< “ nofollow noreferrer”>在这里
You can use a hierarchical query to generate a calendar and the use an
OUTER JOIN
to join it to your data (however, since you are doing it for eachitem
then you probably want aPARTITION
edOUTER JOIN
):Which, for your sample data:
Outputs:
db<>fiddle here