PostgreSQL row_number() over()分组排序
语法:row_number() over( [partition by col1] order by col2[desc])
解释:row_number():为返回的记录定义各行编号;
partition by col1:根据col1进行分组;
order by col2:根据col2进行排序。
举例:
原始数据:
原始数据根据学科分组,按照分数进行排序
根据学科分组,按照分数进行排序select *, row_number() over(partition by course order by score desc) from student;
根据分数排序
根据分数排序select*,row_number() over(order by score desc) from student;
获取每个科目最低分
select * from (select*,row_number() over(partition by course order by score )rank from student) t where rank=1;
获取每个科目最低分好啦,就到这里了,谢谢。
网友评论