求SQL语句

浩子 2006-11-02 01:00:03
有表如下:
Name Score
AA 50
AA -20
AA 40
AA -25

想通过一个SQL语句实现这样的查询:
Name ZScore FScore
AA 90 -45

谢谢
...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackeyabc 2006-11-02
  • 打赏
  • 举报
回复
来晚了,顺便接点分~谢谢~有什么问题再提出来
select name ,sum(case when score>0 then score else 0 end )as ZScore,sum(case when score<0 then score else 0 end )as FScore from tablename group by name
marco08 2006-11-02
  • 打赏
  • 举报
回复
也太快了
marco08 2006-11-02
  • 打赏
  • 举报
回复
create table tb(
name varchar(10),
Score int
)

insert tb select 'AA', 50

union all select 'AA', -20
union all select 'AA', 40
union all select 'AA', -25

select Name,
ZScore=sum( case when Score>0 then Score end),
FScore=sum(case when Score<0 then Score end)
from tb
group by Name

DengXingJie 2006-11-02
  • 打赏
  • 举报
回复
樓上的太正確了
100分
zb63668331 2006-11-02
  • 打赏
  • 举报
回复
select name ,sum(case when score>0 then score else 0 end )as ZScore,sum(case when score<0 then score else 0 end )as FScore from 表名 group by name
OracleRoob 2006-11-02
  • 打赏
  • 举报
回复

create table T(name varchar(100), score int)
insert into T
select 'AA', 50 union all
select 'AA', -20 union all
select 'AA', 40 union all
select 'AA', -25

select
Name,
sum(case when Score>0 then Score else 0 end) as ZScore,
sum(case when Score<0 then Score else 0 end) as FScore
from T
group by Name

drop table T
子陌红尘 2006-11-02
  • 打赏
  • 举报
回复
declare @t table(Name varchar(4),Score int)
insert into @t select 'AA',50
insert into @t select 'AA',-20
insert into @t select 'AA',40
insert into @t select 'AA',-25

select
Name,
ZScore=sum(case when Score>0 then Score else 0 end),
FScore=sum(case when Score<0 then Score else 0 end)
from
@t
group by
Name

/*
Name ZScore FScore
---- ----------- -----------
AA 90 -45
*/
子陌红尘 2006-11-02
  • 打赏
  • 举报
回复
select
Name,
ZScore=sum(case when Score>0 then Score else 0 end),
FScore=sum(case when Score<0 then Score else 0 end)
from

group by
Name

34,592

社区成员

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

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