27,579
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (学号 varchar(3),姓名 varchar(4),专业 varchar(16),科目 varchar(16),成绩 int)
insert into [tb]
select '001','张三','计算机科学与技术','数据库理论与应用',61 union all
select '002','李四','计算机科学与技术','数据库理论与应用',70 union all
select '003','王五','计算机科学与技术','数据库理论与应用',90 union all
select '004','张翔','软件工程','数据库理论与应用',88 union all
select '005','刘鹏','软件工程','数据库理论与应用',32 union all
select '006','陈金','软件工程','数据库理论与应用',95 union all
select '001','张三','计算机科学与技术','网络技术',75 union all
select '002','李四','计算机科学与技术','网络技术',63 union all
select '003','王五','计算机科学与技术','网络技术',95 union all
select '004','张翔','软件工程','网络技术',87 union all
select '005','刘鹏','软件工程','网络技术',65 union all
select '006','陈金','软件工程','网络技术',58
select
专业,科目,
count(1) as 人数,
count(case when 成绩>=90 then 1 end) as [90以上(人)],
count(case when 成绩<90 and 成绩>=80 then 1 end) as [80至90(人],
count(case when 成绩<80 and 成绩>=70 then 1 end) as [70至80(人],
count(case when 成绩<70 and 成绩>=60 then 1 end) as [60至70(人],
count(case when 成绩<60 then 1 end) as [60以下(人)]
from [tb]
group by 专业,科目
drop table [tb]
/*
计算机科学与技术 数据库理论与应用 3 1 0 1 1 0
软件工程 数据库理论与应用 3 1 1 0 0 1
计算机科学与技术 网络技术 3 1 0 1 1 0
软件工程 网络技术 3 0 1 0 1 1
select
专业,科目,count(*)人数,
sum(case when 成绩>=90 then 1 else 0 end) as [90以上],
sum(case when 成绩>=80 and 成绩<90 then 1 else 0 end)as[80以上],
sum(case when 成绩>=70 and 成绩<80 then 1 else 0 end)as[70以上],
sum(case when 成绩>=60 and 成绩<70 then 1 else 0 end)as[60以上],
sum(case when 成绩<60 then 1 else 0 end)as[60以下]
from
tb
group by
专业,科目
select 专业,科目,count(*)人数,sum(case when 成绩>=90 then 1 else 0 end)[90以上],
sum(case when 成绩>=80 and 成绩<90 then 1 else 0 end)[80以上],
sum(case when 成绩>=70 and 成绩<80 then 1 else 0 end)[70以上],
sum(case when 成绩>=60 and 成绩<70 then 1 else 0 end)[60以上],
sum(case when 成绩<60 then 1 else 0 end)[60以下]
from tb
group by 专业,科目
select 专业,科目,
90以上=sum(case when 成绩>90 then 1 else 0 end),
80至90=sum(case when 成绩<90 and 成绩>=80 then 1 else 0 end),
70至80=sum(case when 成绩<80 and 成绩>=70 then 1 else 0 end),
60至70=sum(case when 成绩<=60 and 成绩>=70 then 1 else 0 end),
60以下=sum(case when 成绩<60 then 1 else 0 end)
from tb group by 专业,科目
select 专业,科目
,count(1) as 人数
,sum(case when 成绩>=90 then 1 else 0 end) as [90以上(人)]
,... --其它类似
from tab
group by 专业,科目