一个很纠结的sql问题求助

RxRead 2012-09-01 10:25:37
需要求出平均成绩在0-60,60-80,80-100的人占总人数比例分别是多少。
用SQL语句怎么写啊,求大虾帮助。

平均成绩为这个人的所有成绩除以他的所有科目数量。
有一个数据表如下所示
姓名 课程 成绩
张三 语文 78
张三 数学 94
李斯 语文 75
李斯 英文 80
....

...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
人生无悔 2012-09-01
  • 打赏
  • 举报
回复


with t(姓名,课程,成绩) as(
select '张三','语文',78
union all select '张三','数学',94
union all select '李斯','语文',75
union all select '李斯','英文',80
)
select
convert(varchar(20),convert(numeric(7,2),sum(case when 平均成绩>=0 and 平均成绩<60 then 1 else 0 end)*1.0/count(1)*100))+'%' [0-60占百分比],
convert(varchar(20),convert(numeric(7,2),sum(case when 平均成绩>=60 and 平均成绩<80 then 1 else 0 end)*1.0/count(1)*100))+'%' [60-80占百分比],
convert(varchar(20),convert(numeric(7,2),sum(case when 平均成绩>=80 and 平均成绩<100 then 1 else 0 end)*1.0/count(1)*100))+'%' [80-100占百分比]
from (select avg(成绩) 平均成绩 from t group by 姓名) t1
/*
0-60占百分比 60-80占百分比 80-100占百分比
--------------------- --------------------- ---------------------
0.00% 50.00% 50.00%
*/

22,296

社区成员

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

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