select语句问题
table1:ftype,fval,fgroup三个字段.
现在要select出的结果是:fgroup,type1sum,type2sum
fgroup-是分组字段,type1sum 是ftype=1的fval的和,type2sum是ftype=2的fval的和,ftype只有二个值:1,2
SELECT语句怎么写?
[解决办法]
select
fgroup,
type1sum=sum(case when ftype=1 then fval else 0 end),
type2sum=sum(case when ftype=2 then fval else 0 end)
from table1
group by fgroup
[解决办法]
select fgroup,(case when type=1 then fval else 0) type1sum,
(case when type=2 then fval else 0) type2sum
from table1
group by fgroup,fval
[解决办法]
select fgroup,sum(case ftype when 1 then fval else 0 end)type1sum,
sum(case ftype when 2 then fval else 0 end)type2sum from table1
[解决办法]
sorry group by fgroup
[解决办法]
为挽救自己再写一个
select fgroup,sum(fval*(2-ftype)),sum(fval*(ftype-1)) from #table1 group by fgroup
[解决办法]
不知道这样行不行 请指教 select fgroup from table
group by fgroup
group by fval
[解决办法]
wgzaaa()写的这句有点意思!加上别名就更有意思了!
select fgroup
,sum(fval*(2-ftype)) type1sum
,sum(fval*(ftype-1)) type2sum
from #table1
group by fgroup
[解决办法]
select fgroup,
sum(case ftype when 1 then fval else 0 end) as type1sum,
sum(case ftype when 2 then fval else 0 end) as type2sum
from table1
group by fgroup