日期:2014-05-18 浏览次数:20511 次
declare @表 table (订单编号 varchar(6),订单数量 int,订单时间 datetime,物品编号 varchar(4),交货时间 datetime) insert into @表 select 'A00001',50,'2011-11-02','W001','2011-12-02' union all select 'A00001',60,'2011-12-02','W001','2011-12-07' union all select 'A00001',70,'2012-01-02','W002','2012-02-04' union all select 'A00002',80,'2011-12-02','W002','2011-12-02' union all select 'A00003',90,'2011-12-02','W002','2011-12-02' select 订单编号,物品编号,订单数量=sum(订单数量) from @表 group by 订单编号,物品编号 /* 订单编号 物品编号 订单数量 ------ ---- ----------- A00001 W001 110 A00001 W002 70 A00002 W002 80 A00003 W002 90 */