首页 > 解决方案 > 如何将单个日期列加入时间范围表

问题描述

表 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 个月。

标签: sqljoinself-joinpresto

解决方案


尚未对此进行测试,但可能会有所帮助?

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_cancelsrealdateper分组product


推荐阅读