关于SQL中的CASE的用法,请用学生成绩表来说明?
发布网友
发布时间:2022-04-08 11:23
我来回答
共2个回答
热心网友
时间:2022-04-08 12:52
eclare @t table(id int, name char(10),class char(15),sex char(10))
insert into @t
select 1,'alex','1','man' union all
select 2,'kelly','2','man' union all
select 3,'lily','1','woman' union all
select 4,'judy','1','woman' union all
select 5,'tom','3','man' union all
select 6,'cherly','2','woman' union all
select 7,'cherly','3','woman' union all
select 8,'romeo','2','man'select * from @tselect class,sex ,(case when sex='man' then count(sex) when sex='woman' then count(sex) end) 人数 from @t group by sex,class
(所影响的行数为 8 行)id name class sex
----------- ---------- --------------- ----------
1 alex 1 man
2 kelly 2 man
3 lily 1 woman
4 judy 1 woman
5 tom 3 man
6 cherly 2 woman
7 cherly 3 woman
8 romeo 2 man (所影响的行数为 8 行)class sex 人数
--------------- ---------- -----------
1 man 1
1 woman 2
2 man 2
2 woman 1
3 man 1
3 woman 1(所影响的行数为 6 行)
热心网友
时间:2022-04-08 14:10
select class,sex ,(case when sex='man' then count(sex) when sex='woman' then count(sex) end) 人数 from @t group by sex,class 这个是查出@t这个表的男生和女生的人数各多少个 分数同理