编写一个查询,显示系统中最高的平均进度,并且进度应该四舍五入到小数点后一位

hzbexzde  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(279)
select c.name , round (avg(sC.progress),1) Average_progress
from course c
join studentCourse sC on sC.courseid=c.id
group by c.name
having max avg(sC.progress)

它将作为一个错误出现在“(avg(sc.progress),1)”我做错了什么?

ia2d9nvy

ia2d9nvy1#

你只需要一个 order by 以及 limit :

select c.name, round(avg(sC.progress),1) Average_progress
from course c join
     studentCourse sC
     on sC.courseid = c.id
group by c.name
order by avg(sC.progress) desc
limit 1;
oyjwcjzk

oyjwcjzk2#

你可以做:

select *
from (
  select c.name, round (avg(sC.progress), 1) as Average_progress
  from course c
  join studentCourse sC on sC.courseid = c.id
  group by c.name
) x
order by Average_progress desc
limit 1

结果:

name      Average_progress 
--------- ---------------- 
Course 2             250.5

参见db fiddle的运行示例。

相关问题