&quot年同比使用窗口功能
我正在按年进行Year Year
进行加入分析。我每年都会加入同一张桌子,但是由于我使用了另一个工具来构建我的SQL,它的“动态”不是那么“动态”。如果我可以通过窗口函数解决此问题,那就更好了。因此,任何建议都得到赞赏:D的
想法是按小时进行此操作。也就是说,我想比较销售,从2022-04-05小时8与2021-04-05小时8和2020-04-05小时8的销售额进行比较。
我的数据按小时汇总:
商店 | Timestamp | Sales |
---|---|---|
1 | 2019 -04-05T08:00:00Z | 10000 |
1 | 2020-04-05T08:00:00Z | 12000 |
1 | 2021-04-05T08:00:00Z | 15000 |
1 | 2022-04-05T08:00:00Z | 20000 |
2 | 2019-04-05T08:00 :00z | 13000 |
2 | 2020-04-05T08:00:00 Z | 16000 |
2 | 2021-04-05T08:00:00:00 Z | 19000 |
2 | 2022-04-04-05T08:00:00 Z | 22000 |
值得效果。我添加了它们只是为了澄清:
存储 | TIMESTAMP_1 | SALES_1 | TIMESTAMP_2 | SALES_2 | TIMESTAMP_3 | SALES_3 |
---|---|---|---|---|---|---|
1 | 2019-04-05T08:00:00Z | 10000 | 2020-04-04-05T08:00:00Z | 12000 | 2021-021-04-04-04-05T08:00:00 Z | 15000 |
2 2019 2 | 2019-2 201999 000 2204--04--04--04--04 - 05T08:00:00Z | 13000 | 2020-04-05T08:00:00Z | 16000 | 2021-04-05T08:00:00Z | 19000 |
有什么想法吗? 提前致谢
I'm doing year on year
analysis by doing joins. I'm joining the same table for every year but since I'm using another tool to build my SQL its not so 'dynamic'. If I could solve this with window functions it would be better. So any suggestion is appreciated :D
The idea is to do this by hour. That is, I want to compare sales lets say from 2022-04-05 hour 8 with sales from 2021-04-05 hour 8 and from 2020-04-05 hour 8.
My data is aggregated by hour:
Store | Timestamp | Sales |
---|---|---|
1 | 2019-04-05T08:00:00Z | 10000 |
1 | 2020-04-05T08:00:00Z | 12000 |
1 | 2021-04-05T08:00:00Z | 15000 |
1 | 2022-04-05T08:00:00Z | 20000 |
2 | 2019-04-05T08:00:00Z | 13000 |
2 | 2020-04-05T08:00:00Z | 16000 |
2 | 2021-04-05T08:00:00Z | 19000 |
2 | 2022-04-05T08:00:00Z | 22000 |
Desired result (order may begin with this year) timestamps are not needed. I added them just to clarify:
Store | Timestamp_1 | Sales_1 | Timestamp_2 | Sales_2 | Timestamp_3 | Sales_3 |
---|---|---|---|---|---|---|
1 | 2019-04-05T08:00:00Z | 10000 | 2020-04-05T08:00:00Z | 12000 | 2021-04-05T08:00:00Z | 15000 |
2 | 2019-04-05T08:00:00Z | 13000 | 2020-04-05T08:00:00Z | 16000 | 2021-04-05T08:00:00Z | 19000 |
Any ideas?
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是真正的答案,但如果您只有一天的时间,
则给出:
因此:
确实给出:
如果您有很多月、日、小时的数据,则此方法适用这内部循环:
又名:
给出:
您会注意到,您的示例有 4 年的数据,而您正在丢弃 2022 年的数据。
Not really your answer but if you only have one days worth of hours
gives:
Thus:
does give:
If you have many months, day, hours worth of data this works for the inner loop:
AKA:
gives:
What you will note is you example has 4 years of data, and you are throwing away to 2022 data.