学生成绩分析表的问题

morningnet 2008-03-07 11:49:15
成绩表如:
课程 姓名 成绩

语文 阿三 60
语文 阿斯 40
语文 小岗 85
语文 小方 89
语文 小李 78
语文 阿花 95

要求要统计语文课程的成绩,查询结果如下:
100-90 90-80 80-70 70-60 60-0
1人 2人 1人 2人 1人


语句怎么写?请高手指点.
...全文
593 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
补课 2008-03-08
  • 打赏
  • 举报
回复
课程 100-90 90-80 80-70 70-60 60-0
-------------------- ----------- ----------- ----------- ----------- -----------
数学 1 0 1 0 0
语文 0 2 0 1 0

(2 行受影响)

补课 2008-03-08
  • 打赏
  • 举报
回复

create table 成绩表
(
课程 varchar(20),
姓名 varchar(18),
成绩 int
)

insert into 成绩表
select '语文','阿三',60 union
select '语文','阿斯',40 union
select '语文','小岗',85 union
select '语文','小方',89 union
select '数学','小李',78 union
select '数学','阿花',95


select
课程,
[100-90]=sum(case when 成绩>=90 and 成绩<=100 then 1 else 0 end),
[90-80]=sum(case when 成绩>=80 and 成绩<=90 then 1 else 0 end),
[80-70]=sum(case when 成绩>=70 and 成绩<=80 then 1 else 0 end),
[70-60]=sum(case when 成绩>=60 and 成绩<=70 then 1 else 0 end),
[60-0]=sum(case when 成绩>=60 and 成绩<=0 then 1 else 0 end)

from 成绩表
group by 课程



  • 打赏
  • 举报
回复
select sum(case when 成绩>=90 and 成绩<=100 then 1 else 0 end) as '100-90',
sum(case when 成绩>=80 and 成绩<90 then 1 else 0 end) as '90-80',
sum(case when 成绩>=70 and 成绩<80 then 1 else 0 end) as '80-70',
sum(case when 成绩>=60 and 成绩<70 then 1 else 0 end) as '70-60',
sum(case when 成绩>=0 and 成绩<60 then 1 else 0 end) as '60-0'
from 成绩表
-狙击手- 2008-03-07
  • 打赏
  • 举报
回复
select 
[100-90] = sum(case when 成绩 between 90 and 100 then 1 else 0 end),
[90-80] = sum(case when 成绩 between 90 and 80 then 1 else 0 end),
[80-70] = sum(case when 成绩 between 70 and 80 then 1 else 0 end),
[70-60] = sum(case when 成绩 between 60 and 70 then 1 else 0 end),
[60-0] = sum(case when 成绩 between 0 and 60 then 1 else 0 end)
from t
where 课程 = '语文'
dawugui 2008-03-07
  • 打赏
  • 举报
回复
create table tb(课程 varchar(10) , 姓名 varchar(10) , 成绩 int)
insert into tb values('语文', '阿三', 60 )
insert into tb values('语文', '阿斯', 40 )
insert into tb values('语文', '小岗', 85 )
insert into tb values('语文', '小方', 89 )
insert into tb values('语文', '小李', 78 )
insert into tb values('语文', '阿花', 95 )
go

select
sum(case when 成绩 between 90 and 100 then 1 else 0 end) [100-90],
sum(case when 成绩 between 80 and 89 then 1 else 0 end) [90-80],
sum(case when 成绩 between 70 and 79 then 1 else 0 end) [80-70],
sum(case when 成绩 between 60 and 69 then 1 else 0 end) [70-60],
sum(case when 成绩 < 60 then 1 else 0 end) [60-0]
from tb

drop table tb

/*
100-90 90-80 80-70 70-60 60-0
----------- ----------- ----------- ----------- -----------
1 2 1 1 1

(所影响的行数为 1 行)
*/
dawugui 2008-03-07
  • 打赏
  • 举报
回复
成绩表如:
课程 姓名 成绩

语文 阿三 60
语文 阿斯 40
语文 小岗 85
语文 小方 89
语文 小李 78
语文 阿花 95

要求要统计语文课程的成绩,查询结果如下:
100-90 90-80 80-70 70-60 60-0
1人 2人 1人 2人 1人


语句怎么写?请高手指点.

select 
sum(case when 成绩 between 90 and 100 then 1 else 0 end) [100-90],
sum(case when 成绩 between 80 and 89 then 1 else 0 end) [90-80],
sum(case when 成绩 between 70 and 79 then 1 else 0 end) [80-70],
sum(case when 成绩 between 60 and 69 then 1 else 0 end) [70-60],
sum(case when 成绩 < 60 then 1 else 0 end) [60-0]
from tb

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧