Hive分析窗口函数(二) NTILE,ROW_NUMBER,RANK,DENSE_RANK

本文中介绍前几个序列函数,NTILE,ROW_NUMBER,RANK,DENSE_RANK,下面会一一解释各自的用途。

Hive版本为 apache-hive-0.13.1

注意: 序列函数不支持WINDOW子句。(什么是WINDOW子句,点此查看前面的文章

数据准备:

 
 
  1. cookie1,2015-04-10,1
  2. cookie1,2015-04-11,5
  3. cookie1,2015-04-12,7
  4. cookie1,2015-04-13,3
  5. cookie1,2015-04-14,2
  6. cookie1,2015-04-15,4
  7. cookie1,2015-04-16,4
  8. cookie2,2015-04-10,2
  9. cookie2,2015-04-11,3
  10. cookie2,2015-04-12,5
  11. cookie2,2015-04-13,6
  12. cookie2,2015-04-14,3
  13. cookie2,2015-04-15,9
  14. cookie2,2015-04-16,7
  15.  
  16. CREATE EXTERNAL TABLE lxw1234 (
  17. cookieid string,
  18. createtime string, --day
  19. pv INT
  20. ) ROW FORMAT DELIMITED
  21. FIELDS TERMINATED BY ','
  22. stored as textfile location '/tmp/lxw11/';
  23.  
  24. DESC lxw1234;
  25. cookieid STRING
  26. createtime STRING
  27. pv INT
  28.  
  29. hive> select * from lxw1234;
  30. OK
  31. cookie1 2015-04-10 1
  32. cookie1 2015-04-11 5
  33. cookie1 2015-04-12 7
  34. cookie1 2015-04-13 3
  35. cookie1 2015-04-14 2
  36. cookie1 2015-04-15 4
  37. cookie1 2015-04-16 4
  38. cookie2 2015-04-10 2
  39. cookie2 2015-04-11 3
  40. cookie2 2015-04-12 5
  41. cookie2 2015-04-13 6
  42. cookie2 2015-04-14 3
  43. cookie2 2015-04-15 9
  44. cookie2 2015-04-16 7

NTILE

NTILE(n),用于将分组数据按照顺序切分成n片,返回当前切片值
NTILE不支持ROWS BETWEEN,比如 NTILE(2) OVER(PARTITION BY cookieid ORDER BY createtime ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)
如果切片不均匀,默认增加第一个切片的分布

 
 
  1. SELECT
  2. cookieid,
  3. createtime,
  4. pv,
  5. NTILE(2) OVER(PARTITION BY cookieid ORDER BY createtime) AS rn1, --分组内将数据分成2
  6. NTILE(3) OVER(PARTITION BY cookieid ORDER BY createtime) AS rn2, --分组内将数据分成3
  7. NTILE(4) OVER(ORDER BY createtime) AS rn3 --将所有数据分成4
  8. FROM lxw1234
  9. ORDER BY cookieid,createtime;
  10.  
  11. cookieid day pv rn1 rn2 rn3
  12. -------------------------------------------------
  13. cookie1 2015-04-10 1 1 1 1
  14. cookie1 2015-04-11 5 1 1 1
  15. cookie1 2015-04-12 7 1 1 2
  16. cookie1 2015-04-13 3 1 2 2
  17. cookie1 2015-04-14 2 2 2 3
  18. cookie1 2015-04-15 4 2 3 3
  19. cookie1 2015-04-16 4 2 3 4
  20. cookie2 2015-04-10 2 1 1 1
  21. cookie2 2015-04-11 3 1 1 1
  22. cookie2 2015-04-12 5 1 1 2
  23. cookie2 2015-04-13 6 1 2 2
  24. cookie2 2015-04-14 3 2 2 3
  25. cookie2 2015-04-15 9 2 3 4
  26. cookie2 2015-04-16 7 2 3 4

–比如,统计一个cookie,pv数最多的前1/3的天

 
 
  1. SELECT
  2. cookieid,
  3. createtime,
  4. pv,
  5. NTILE(3) OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn
  6. FROM lxw1234;
  7.  
  8. --rn = 1 的记录,就是我们想要的结果
  9.  
  10. cookieid day pv rn
  11. ----------------------------------
  12. cookie1 2015-04-12 7 1
  13. cookie1 2015-04-11 5 1
  14. cookie1 2015-04-15 4 1
  15. cookie1 2015-04-16 4 2
  16. cookie1 2015-04-13 3 2
  17. cookie1 2015-04-14 2 3
  18. cookie1 2015-04-10 1 3
  19. cookie2 2015-04-15 9 1
  20. cookie2 2015-04-16 7 1
  21. cookie2 2015-04-13 6 1
  22. cookie2 2015-04-12 5 2
  23. cookie2 2015-04-14 3 2
  24. cookie2 2015-04-11 3 3
  25. cookie2 2015-04-10 2 3

ROW_NUMBER

ROW_NUMBER() –从1开始,按照顺序,生成分组内记录的序列
–比如,按照pv降序排列,生成分组内每天的pv名次
ROW_NUMBER() 的应用场景非常多,再比如,获取分组内排序第一的记录;获取一个session中的第一条refer等。

 

 
 
  1. SELECT
  2. cookieid,
  3. createtime,
  4. pv,
  5. ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn
  6. FROM lxw1234;
  7.  
  8. cookieid day pv rn
  9. -------------------------------------------
  10. cookie1 2015-04-12 7 1
  11. cookie1 2015-04-11 5 2
  12. cookie1 2015-04-15 4 3
  13. cookie1 2015-04-16 4 4
  14. cookie1 2015-04-13 3 5
  15. cookie1 2015-04-14 2 6
  16. cookie1 2015-04-10 1 7
  17. cookie2 2015-04-15 9 1
  18. cookie2 2015-04-16 7 2
  19. cookie2 2015-04-13 6 3
  20. cookie2 2015-04-12 5 4
  21. cookie2 2015-04-14 3 5
  22. cookie2 2015-04-11 3 6
  23. cookie2 2015-04-10 2 7

RANK 和 DENSE_RANK

—RANK() 生成数据项在分组中的排名,排名相等会在名次中留下空位
—DENSE_RANK() 生成数据项在分组中的排名,排名相等会在名次中不会留下空位

 

 
 
  1. SELECT
  2. cookieid,
  3. createtime,
  4. pv,
  5. RANK() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn1,
  6. DENSE_RANK() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn2,
  7. ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn3
  8. FROM lxw1234
  9. WHERE cookieid = 'cookie1';
  10.  
  11. cookieid day pv rn1 rn2 rn3
  12. --------------------------------------------------
  13. cookie1 2015-04-12 7 1 1 1
  14. cookie1 2015-04-11 5 2 2 2
  15. cookie1 2015-04-15 4 3 3 3
  16. cookie1 2015-04-16 4 3 3 4
  17. cookie1 2015-04-13 3 5 4 5
  18. cookie1 2015-04-14 2 6 5 6
  19. cookie1 2015-04-10 1 7 6 7
  20.  
  21. rn1: 15号和16号并列第3, 13号排第5
  22. rn2: 15号和16号并列第3, 13号排第4
  23. rn3: 如果相等,则按记录值排序,生成唯一的次序,如果所有记录值都相等,或许会随机排吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值