第一个表是数据源
第二表是结果
--思路,
--1 用when then 语句,将成绩列按条件拆分成两列
--2 用集合函数,分别求两列的个数 count
select teamName ,
胜=count( case [gameResult]
when '胜' then 1
end),
负=count(case [gameResult]
when '负' then 1
end)
from TeamScore
group by teamName
数据源
--- 数据源
create table TeamScore
(
id int identity primary key not null,
teamName nvarchar(32) not null,
gameResult nchar(2) not null
)
go
insert into TeamScore values (N'公牛',N'胜')
insert into TeamScore values (N'小牛',N'胜')
insert into TeamScore values (N'奇才',N'负')
insert into TeamScore values (N'湖人',N'胜')
insert into TeamScore values (N'公牛',N'胜')
insert into TeamScore values (N'公牛',N'胜')
insert into TeamScore values (N'奇才',N'负')
insert into TeamScore values (N'公牛',N'负')