sql子查询,分别统计出每支队伍的胜负次数

来源:百度知道 编辑:UC知道 时间:2024/06/04 05:55:41
我的表结构如下
duiwu shengfu
1 胜
2 胜
1 胜
1 负
2 负
1 胜
2 负
要查询出如下结果:
duiwu 胜 负
1 3 1
2 1 2
就是分别统计出每支队伍的胜负次数,这样的sql语句该怎么写啊?大虾帮帮忙啊,谢谢了

select a.duiwu ,a.胜,b.负 from
(select count(*) 胜, duiwu from tabb where shengfu='胜' group by duiwu ) a left join
(select count(*) 负, duiwu from tabb where shengfu='负' group by duiwu ) b on a.duiwu=b.duiwu

--sql2000调试通过,可以自己用case when改写一下

select duiwu ,
sum(case when shengfu = '胜' then 1 else 0 end) win,
sum(case when shengfu = '负' then 1 else 0 end) lost
from 表
group by duiwu ;

ok!

如果你用的是ACCESS数据库,可以用以下查询语句:
select duiwu,sum(iif(shengfu="胜",1,0)) as 胜,sum(iif(shengfu="负",1,0)) as 负 from 表名 group by duiwu

select ID, count(decode(VORF,'won',1,0)) as won, count(decode(VORF,'lost',1,0) as lost
from tA

if SQL server just use case when is ok

大家已经回答的很好了