Hive SQL rank()/dense_rank()/row_number()的区别

SET mapreduce.job.queuename=xxx;

WITH a AS (
SELECT 'a' col_1, 1 col_2
union all
SELECT 'a' col_1, 2 col_2
union all
SELECT 'a' col_1, 3 col_2
),

b AS (
SELECT 'a' col_1, 4 col_2
union all
SELECT 'a' col_1, 4 col_2
union all
SELECT 'a' col_1, 5 col_2
),

c AS (SELECT * from a UNION ALL SELECT * FROM b)

SELECT *,
rank() OVER(PARTITION BY col_1 ORDER BY col_2 DESC) rnk,  -- 排名相等,排名会留出空位
dense_rank() OVER(PARTITION BY col_1 ORDER BY col_2 DESC) dense_rnk,  -- 排名相等,排名不会留出空位
row_number() OVER(PARTITION BY col_1 ORDER BY col_2 DESC) row_rnk  -- 直接按照记录值排序,如果两个值相等则随机排
FROM c;

posted @ 2021-05-11 16:21  swordspoet  阅读(330)  评论(0编辑  收藏  举报