本帖最后由 王洪磊 于 2013-6-9 09:18 编辑
有一张表T-Score
Date Name Score
2008-8-8 拜仁 胜
2008-8-9 奇才 胜
2008-8-9 湖人 胜
2008-8-10 拜仁 负
2008-8-8 拜仁 负
2008-8-12 奇才 胜
要求输出下面格式:
Name 胜 负
拜仁 1 2
湖人 1 0
奇才 2 0
注意:在字符串前加N,比如N'胜'
本人自己写的,但是感觉代码好长,是不是有更好更短的写法呢?求指导
select distinct Name,
(case Name
when N'拜仁' then (select count(*) from TableScore where Score='胜' and Name='拜仁')
when N'湖人' then (select count(*) from TableScore where Score='胜' and Name='湖人')
when N'奇才' then (select count(*) from TableScore where Score='胜' and Name='奇才')
end
) as 胜,
(case Name
when N'拜仁' then (select count(*) from TableScore where Score='负' and Name='拜仁')
when N'湖人' then (select count(*) from TableScore where Score='负' and Name='湖人')
when N'奇才' then (select count(*) from TableScore where Score='负' and Name='奇才')
end
) as 负
from TableScore
|