Rank的基本语法为:
RANK ( ) OVER ( [query_partition_clause] order_by_clause )
用于分组排序
学生成绩表score现在有以下字段和数据:
name type score
小黑 语文 80
小黑 语文
77
小黑 语文
90
小黑 语文
78
小黑 语文
69
王二 语文
83
王二 语文
88
王二 语文
75
王二 语文
93
王二 语文
78
要查询出每个人语文考得最好的三次
select * from (select rank() over(partition by name order by score asc) rk,s.* from score s) where rk<=3
查询结果如下:
rk name type score
1 小黑 语文 90
2 小黑
语文 80
3 小黑 语文 78
1 王二
语文 93
2 王二
语文 88
3 王二
语文 83