表
fgj pjlx
10 pj
20 kwy
30 yz
40 qhc
50 qi
60 imw
...
select sum(fgj) from tb where pjlx='pj'
select sum(fgj) from tb where pjlx='kwy'
select sum(fgj) from tb where pjlx='yz'
select sum(fgj) from tb where pjlx='qhc'
我想要把这结果变成
PJ KWY+YZ+QHC10 90=20+30+40
如何写SQL语句呀
fgj pjlx
10 pj
20 kwy
30 yz
40 qhc
50 qi
60 imw
...
select sum(fgj) from tb where pjlx='pj'
select sum(fgj) from tb where pjlx='kwy'
select sum(fgj) from tb where pjlx='yz'
select sum(fgj) from tb where pjlx='qhc'
我想要把这结果变成
PJ KWY+YZ+QHC10 90=20+30+40
如何写SQL语句呀
PJ = (select sum(fgj) from tb where pjlx='pj' ),
[KWY+YZ+QHC] = (select sum(fgj) from tb where pjlx in ('kwy' , 'yz' , 'qhc' )
from
(select id=1,fgj=sum(fgj) from tb where pjlx='pj' ) a,
(select id=1,fgj=sum(fgj) from tb where pjlx='kwy' ) b,
(select id=1,fgj=sum(fgj) from tb where pjlx='yz' ) c,
(select id=1,fgj=sum(fgj) from tb where pjlx='qhc') d
where a.id=b.id and b.id=c.id and c.id=d.id这样?
insert into tb values(10 , 'pj')
insert into tb values(20 , 'kwy')
insert into tb values(30 , 'yz')
insert into tb values(40 , 'qhc')
insert into tb values(50 , 'qi')
insert into tb values(60 , 'imw')
goselect
PJ = (select sum(fgj) from tb where pjlx='pj' ),
[KWY+YZ+QHC] = (select sum(fgj) from tb where pjlx in ('kwy' , 'yz' , 'qhc' ))drop table tb /*
PJ KWY+YZ+QHC
----------- -----------
10 90(所影响的行数为 1 行)
*/
insert into tb values(10 , 'pj')
insert into tb values(20 , 'kwy')
insert into tb values(30 , 'yz')
insert into tb values(40 , 'qhc')
insert into tb values(50 , 'qi')
insert into tb values(60 , 'imw')
go
select a.fgj PJ,b.fgj+c.fgj+d.fgj [KWY+YZ+QHC]
from
(select id=1,fgj=sum(fgj) from tb where pjlx='pj' ) a,
(select id=1,fgj=sum(fgj) from tb where pjlx='kwy' ) b,
(select id=1,fgj=sum(fgj) from tb where pjlx='yz' ) c,
(select id=1,fgj=sum(fgj) from tb where pjlx='qhc') d
where a.id=b.id and b.id=c.id and c.id=d.id
/*----
PJ KWY+YZ+QHC
----------- -----------
10 90(1 行受影响)
----*/借老D的表我也测试了一下.