sql - 如何将单个日期列加入时间范围表
问题描述
表 1 表列是:
cancel_date product total_cancels
6/1/2017 a 100
6/1/2017 b 40
6/2/2017 b 10
6/3/2017 b 20
.
.
.
6/1/2018 a 40
6/1/2018 b 10
表 2
realdate
6/1/2017
6/2/2017
6/3/2017
.
.
.
6/1/2018
我想得到什么
product realdate total_cancels cancel_date
a 6/1/2017 100000 6/1/2016-4/30/2017
b 6/1/2017 8000 6/1/2016-4/30/2017
a 6/2/2017 100000 6/2/2016-5/1/2017
b 6/2/2017 8000 6/2/2016-5/1/2017
...
所以基本上按 realdate 对 total_cancels 求和,对于每个 realdate,我需要将 canceldate 分组为 2-12 个月。
解决方案
尚未对此进行测试,但可能会有所帮助?
select t1.product, t2.realdate, sum(t1.total_cancels)
from t1 join t2 on t1.cancel_date=t2.realdate
group by t1.product, t2.realdate
而且我不确定我是否理解您在最后一栏中想要的内容。这只是total_cancels
按realdate
per分组product
。
推荐阅读
- electron - 无法为小型转储文件获取匹配的电子符号
- c# - 如何通过可空字段查询 Azure 表存储?
- jquery - 控制台中位置 1 的 JSON 中的意外标记 o
- javascript - 为什么 Object.create 会干扰 Object.keys、Object.values 和 Object.entries 的返回值?
- webpack - 如何为 SASS 设置 Webpack
- arrays - 我们如何在数组中找到唯一的对?
- php - 我不希望页面在 php 执行后刷新
- windows - 为什么 XCOPY /W 和 REPLACE /W 会为他们的单字符提示使用所有重定向的文本数据?
- ios - iOS 应用程序能否以编程方式将 Apple Watch 置于飞行模式?
- java - 将 Erlang 与 Java 库链接