SQL语句 如何通过一个SQL语句,将一列的成绩 分成胜,负(A,B)(及格不及格)两列显示出来

第一个表是数据源
第二表是结果

在这里插入图片描述

--思路,
--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'负')
发布了55 篇原创文章 · 获赞 4 · 访问量 1429

猜你喜欢

转载自blog.csdn.net/BowenXu11/article/details/104700349