这样的语句怎么写?
表:
TA TB TC TD TE
A B C 2 120.00
A B D 5 600.00
A B1 C 3 360.00
A B1 D 4 480.00
查询出来的结果:
TA TB TC TD TE TF TG
A B C 2 120.00
A B D 5 600.00 7 720.00
A B1 C 3 360.00
A B1 D 4 480.00 7 7200.00
这样的SQL语句怎样写?
------解决方案--------------------if object_id( 'pubs..tb ') is not null
drop table tb
go
create table tb(
TA varchar(10),TB varchar(10),TC varchar(10),TD int,TE int
)
insert into tb(TA,TB,TC,TD,TE) values( 'A ', 'B ' , 'C ', 2, 120)
insert into tb(TA,TB,TC,TD,TE) values( 'A ', 'B ' , 'D ', 5, 600)
insert into tb(TA,TB,TC,TD,TE) values( 'A ', 'B1 ', 'C ', 3, 360)
insert into tb(TA,TB,TC,TD,TE) values( 'A ', 'B1 ' , 'D ', 4, 480)
select a.* , b.td,b.te from tb a,
(select ta,tb,sum(td) td,sum(te) te from tb group by ta,tb) b
where a.ta = b.ta and a.tb = b.tb
drop table tb
/*
TA TB TC TD TE td te
---------- ---------- ---------- ----------- ----------- ----------- -----------
A