测试数据
1 A 1
1 A 2
1 B 3
2 B 11
2 D 12
2 A 13
3 B 21
3 F 22
3 A 23
4 B 36
4 A 37
1 G 91
2 A 99
3 D 93
4 E 94
①.row_number() over(partition by X1 order by X2 ) as rank
对表中的数据按照X1分组,按照X2排序,对每个分组内的数据标号,每个分组内的 标号是连续的,标号在每个分组内从1开始。
session.sql(
"""
| select
| riqi,leibie,jine,row_number() over(partition by leibie order by jine desc ) as rank
| from t order by leibie
""".stripMargin).show(100)
结果
+----+------+----+----+
|riqi|leibie|jine|rank|
+----+------+----+----+
| 2| A|99.0| 1|
| 4| A|37.0| 2|
| 3| A|23.0| 3|
| 2| A|13.0| 4|
| 1| A| 2.0| 5|
| 1| A| 1.0| 6|
| 2| B|11.0| 3|
| 1| B| 3.0| 4|
| 3| B|21.0| 2|
| 4| B|36.0| 1|
| 2| D|12.0| 2|
| 3| D|93.0| 1|
| 4| E|94.0| 1|
| 3| F|22.0| 1|
| 1| G|91.0| 1|
+----+------+----+----+
②. rank() over(partition by X1 order by X2 ) as rank
对表中的数据按照X1分组,按照X2排序,对每个分组内的数据标号,每个分组内的 标号不连续且相同的数据标号相同,标号在每个分组内从1开始。
session.sql(
"""
| select
| riqi,leibie,jine,rank() over(partition by leibie order by jine desc ) as rank
| from t order by leibie
""".stripMargin).show(100)
③.dense_rank() over(partition by X1 order by X2 ) as rank
对表中的数据按照X1分组,按照X2排序,对每个分组内的数据标号,每个分组内的标号连续且相同的数据标号相同,标号在每个分组内从1开始。
session.sql(
"""
| select
| riqi,leibie,jine,dense_rank() over(partition by leibie order by jine desc ) as rank
| from t order by leibie
""".stripMargin).show(100)