假设有一个原始数据表如下:
Date | ID | Stats
9/12 | A | 10
9/13 | A | 20
9/13 | B | 5
9/14 | B | 3
9/15 | A | 100
9/16 | A | 200
9/17 | A | 250
我想通过SQL查询创建一个新表,如下所示:
Start_Date | End_Date | ID | Sum(Stats)
9/12 | 9/13 | A | 30
9/13 | 9/14 | B | 8
9/15 | 9/17 | A | 550
在这种情况下,我该如何编写SELECT查询?
答案 0 :(得分:1)
它正在我的测试中工作:
select
alias1.DATA,
alias1.DATA + interval 1 day ,
alias2.ID,
sum(alias2.stats)
from
TEST as alias1 inner join
TEST as alias2 on (alias2.data >= alias1.data and alias2.data <= alias1.data + interval 1 day)
group by alias1.DATA,alias1.DATA + interval 1 day,alias2.ID