For SQL SERVER:
declare @t3 table (buyer nvarchar(50),goods nvarchar(50),qty int)
insert into @t3 values('A','甲',2)
insert into @t3 values('A','甲',3)
insert into @t3 values('B','乙',4)
insert into @t3 values('C','丙',1)
insert into @t3 values('C','丙',2)
insert into @t3 values('A','丁',2)
insert into @t3 values('B','丙',5)
select * from @t3
select t1.buyer,t1.goods,SUM(t1.qty) from @t3 t1 inner join @t3 t2 on t1.buyer=t2.buyer and t1.goods <> t2.goods
group by t1.buyer,t1.goods
回复 更多评论