如何将单个日期列加入时间范围 table
how to join a single date column to a time rage table
table 1 table 列是:
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
table 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
...
所以基本上按重新日期对 total_cancels 求和,对于每个重新日期,我需要将取消日期分组为 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
而且我不确定我是否理解您在最后一栏中想要的内容。这只是根据 product
.
按 realdate
分组 total_cancels
使用聚合函数 sum
。但奇怪的是,您显示输出的方式取消了日期列。我认为这是你的打字错误
select t1.product, t2.realdate, sum(t1.total_cancels) as total_cancels
from Table1 t1 inner join Table2 t2 on date(t1.cancel_date)=date(t2.realdate)
group by t1.product, t2.realdate
这个问题好像和我在这里回答的一样:
join start_date and _end_date to another table and sum up
您要做的是将 table_2 加入 table_1,使用条件 table_1。 cancel_date 介于 table_2.cancel_start_date 和 table_2.cancel_end_date 之间。但首先我们需要使用 DATE_PARSE 函数来使日期具有可比性。最后简单总结一下数值。
SELECT
table_1.product,
table_2.realdate,
SUM(total_cancels) AS total_cancels,
CONCAT(table_2.cancel_start_date, '-', table_2.cancel_end_date) as start_to_end
FROM table_1
JOIN table_2
WHERE DATE_PARSE(table_1. cancel_date, '%m/%d/%Y')
BETWEEN DATE_PARSE(table_2.cancel_start_date, '%m/%d/%Y')
AND DATE_PARSE(table_2.cancel_end_date, '%m/%d/%Y')
GROUP BY 1, 2, 4
table 1 table 列是:
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
table 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
...
所以基本上按重新日期对 total_cancels 求和,对于每个重新日期,我需要将取消日期分组为 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
而且我不确定我是否理解您在最后一栏中想要的内容。这只是根据 product
.
realdate
分组 total_cancels
使用聚合函数 sum
。但奇怪的是,您显示输出的方式取消了日期列。我认为这是你的打字错误
select t1.product, t2.realdate, sum(t1.total_cancels) as total_cancels
from Table1 t1 inner join Table2 t2 on date(t1.cancel_date)=date(t2.realdate)
group by t1.product, t2.realdate
这个问题好像和我在这里回答的一样: join start_date and _end_date to another table and sum up
您要做的是将 table_2 加入 table_1,使用条件 table_1。 cancel_date 介于 table_2.cancel_start_date 和 table_2.cancel_end_date 之间。但首先我们需要使用 DATE_PARSE 函数来使日期具有可比性。最后简单总结一下数值。
SELECT
table_1.product,
table_2.realdate,
SUM(total_cancels) AS total_cancels,
CONCAT(table_2.cancel_start_date, '-', table_2.cancel_end_date) as start_to_end
FROM table_1
JOIN table_2
WHERE DATE_PARSE(table_1. cancel_date, '%m/%d/%Y')
BETWEEN DATE_PARSE(table_2.cancel_start_date, '%m/%d/%Y')
AND DATE_PARSE(table_2.cancel_end_date, '%m/%d/%Y')
GROUP BY 1, 2, 4