编号 品名 数量 日期 供商
1011 a 10 2009-01-01 001
1011 a 1 2009-01-01 002
1011 a 3 2009-01-03 001
1012 b 3 2009-01-01 001
1012 b 5 2009-01-02 003
1012 b 5 2009-01-02 003
我想得到以下结果:
编号 品名 数量 日期 供商
1011 a 14 2009-01-03 001
1012 b 13 2009-01-02 003
SQL语句
Select distinct
a.编号,
a.品名,
b.数量,
a.日期,
a.供商
from
tb a,
(select 编号,sum(数量) as 数量 from tb group by 编号) b
where
a.编号=b.编号
and
not exists(select 1 from tb where 编号=a.编号 and 日期>a.日期)