group by是分组函数,
partition by是分区函数
sum()是聚合函数
over函数的写法: 先对cno 中相同的进行分区,在cno 中相同的情况下对degree 进行排序
over(partition by cno order by degree )
分区函数Partition By与rank()的用法“对比”分区函数Partition By与row_number()的用法
例:查询每名课程的第一名的成绩
(1)使用rank()
SELECT *
FROM (select sno,cno,degree,
rank()over(partition by cno order by degree desc) mm
from score)
where mm = 1;
得到结果:
(2)使用row_number()
SELECT *
FROM (select sno,cno,degree,
row_number()over(partition by cno order by degree desc) mm
from score)
where mm = 1;
得到结果:
区别
由以上的例子得出&