我在SQLITE中有这样的表现。我必须找到该日期之前四周总和(数量)的总和。
Date|Quantity
------|------
4/22/2015|23
4/25/2015|10
5/1/2015| 28
6/9/2015|45
6/29/2015|31
预期结果:
Date | Quantity
------ | ------
4/22/2015 | 23
4/25/2015 | 33
5/1/2015 | 38
6/9/2015 | 45
6/29/2015 | 76
我正在尝试使用以下查询。我没有得到正确的结果。
select t1.date, t1.quantity, SUM(t2.quantity) as sum
from aaa t1 left join
aaa t2
on datetime(t1.date) <= datetime(t2.date, '-28 days')
group by t1.date, t1.quantity
order by t1.date
答案 0 :(得分:1)
我认为你很亲密。日期比较需要开始和结束:
select t1.date, t1.quantity, SUM(t2.quantity) as sum
from aaa t1 left join
aaa t2
on datetime(t2.date) >= datetime(t1.date, '-28 days') and
datetime(t2.date) <= datetime(t1.date)
group by t1.date, t1.quantity
order by t1.date;