开窗函数使用

SQL里开窗函数灵活运用以后可以给数据的计算提供很大的便利性,话不多说,直接进入正题,表结构如下:
开窗函数实现累加
对照这个表我想知道每个学生的总成绩,各科目的最高分,每个班级的平均成绩,这时用开窗函数就可以一次性求出来。
SELECT DISTINCT stuid,stuname,class,course,sum(score) over(partition by stuid,stuname) sum_score,
max(score) over(partition by course) maxcourse_score,avg(score) over(partition by class)
from student_score;

结果:
开窗函数实现累加
接下来说下累加的使用,这里就不重新建表了,直接用上面的那个表,每个人学生 课程成绩累加:

SELECT DISTINCT stuid,stuname,course,score,sum(score) over(partition by stuid,stuname order by course) leijia
from student_score ;

结果:
开窗函数实现累加

相关文章:

  • 2021-08-05
  • 2021-07-06
  • 2021-04-24
  • 2021-12-05
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-08-01
猜你喜欢
  • 2022-12-23
  • 2021-08-01
  • 2021-12-19
  • 2021-08-01
  • 2021-12-16
  • 2021-10-12
相关资源
相似解决方案