我想编写一个查询并在其中使用子查询,结果集将是某些日期内已售商品的数量。
表格如下:
item_name sold date
------------------------------------
A 20 10.1
B 10 10.3
A 10 10.5
C 20 10.4
A 30 10.8
,结果集如下:
item_name sold date
-------------------------------------
A 20 10.1
B 10 10.3
A 30 10.5
C 20 10.4
A 60 10.8
答案 0 :(得分:2)
select t1.item_name, sum(t1.sold) as sold, t2.date
from myTable t1
inner join myTable t2 on (t1.item_name = t2.item_name) and (t1.date <= t2.date)
group by t1.item_name, t2.date
答案 1 :(得分:2)
您可以使用以下查询
select t1.item_name,
(select sum(sold) from table1 t2 where t2.date <= t1.date
and t2.item_name = t1.item_name ) as sold,
t1.date
from table1 t1
不是这么简单...... sql是非常简单的语言......只需要用冷静和冷静的思维来思考你的问题:)