22,210
社区成员
发帖
与我相关
我的任务
分享
--1.用sql语句把(1)班的各科成绩都大于83的学生按三科总成绩的平均分由高到低排列,应该如何写sql语句?
select * from(
select 姓名,avg(成绩) as 平均成绩
from 表名 k
where 班级='(1)班'
and not exists(select * from 表名 where k.班级=班级 and k.姓名=姓名 and 成绩<=83 )) k
order by 平均成绩 desc
use jiudian
go
create table tb
(
班级 nvarchar (100)
,姓名 nvarchar(100)
,科目 nvarchar(100)
,成绩 int
)
insert tb
select '(1)班', '小明', '语文', 85
union all select '(1)班', '小明', '数学', 90
union all select '(1)班', '小明', '英语', 82
union all select '(1)班', '小兰', '语文', 83
union all select '(1)班', '小兰', '数学', 95
union all select '(1)班', '小兰', '英语', 85
union all select '(1)班', '小欢', '语文', 86
union all select '(1)班', '小欢', '数学', 91
union all select '(1)班', '小欢', '英语', 86
union all select '(2)班', '小花', '语文', 87
union all select '(2)班', '小花', '数学', 89
union all select '(2)班', '小花', '英语', 89
select 姓名,avg(成绩) 平均成绩 from tb where 班级='(1)班'
group by 姓名 having min(成绩)>83
order by 平均成绩 desc
not exists(select * from 表名 where k.班级=班级 and k.姓名=姓名 and 成绩<=83 )
这个就是保证 这个人不会有成绩低于83分的
select * from(
select 姓名,avg(成绩) as 平均成绩
from 表名 k
where 班级='(1)班'
and not exists(select * from 表名 where k.班级=班级 and k.姓名=姓名 and 成绩<=83 )
group by 姓名) k
order by 平均成绩 desc