select语句问题

zly1980 2007-05-25 12:14:18
table1:ftype,fval,fgroup三个字段.
现在要select出的结果是:fgroup,type1sum,type2sum

fgroup-是分组字段,type1sum 是ftype=1的fval的和,type2sum是ftype=2的fval的和,ftype只有二个值:1,2
SELECT语句怎么写?
...全文
210 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wgzaaa 2007-05-25
  • 打赏
  • 举报
回复
为挽救自己再写一个
select fgroup,sum(fval*(2-ftype)),sum(fval*(ftype-1)) from #table1 group by fgroup
wgzaaa 2007-05-25
  • 打赏
  • 举报
回复
sorry group by fgroup
wgzaaa 2007-05-25
  • 打赏
  • 举报
回复
我的有问题吗
Andy-W 2007-05-25
  • 打赏
  • 举报
回复
1楼正解,1楼速度真快
wgzaaa 2007-05-25
  • 打赏
  • 举报
回复

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
肥胖的柠檬 2007-05-25
  • 打赏
  • 举报
回复
...怪自己打字慢..
肥胖的柠檬 2007-05-25
  • 打赏
  • 举报
回复
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
chuifengde 2007-05-25
  • 打赏
  • 举报
回复
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
sjlion 2007-05-25
  • 打赏
  • 举报
回复
顶楼上,创意不错
david0927cs2006 2007-05-25
  • 打赏
  • 举报
回复
呵呵,和前面的重复了
david0927cs2006 2007-05-25
  • 打赏
  • 举报
回复
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

EmeraldSword 2007-05-25
  • 打赏
  • 举报
回复
wgzaaa()写的这句有点意思!加上别名就更有意思了!

select fgroup
,sum(fval*(2-ftype)) type1sum
,sum(fval*(ftype-1)) type2sum
from #table1
group by fgroup
mao5588 2007-05-25
  • 打赏
  • 举报
回复
不知道这样行不行 请指教 select fgroup from table
group by fgroup
group by fval
VCLIFE 2007-05-25
  • 打赏
  • 举报
回复
真快

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧