大数据 | 23年2月近期常用Hive SQL写法

148 阅读1分钟

Hive 字符串转数组

SELECT split('1,2,3,4,5',',')

Hive 行转列

字符串转数组,转列
SELECT explode(split('1,2,3,4,5',','))
字符串转数组,转列,转表
SELECT \* from (SELECT explode(split('1,2,3,4,5',','))) t

参考网址

Hive 字符串查找函数

SELECT locate('zhang', 'zhangsan')

参考网址

hive 的开窗函数row_number() over()

SELECT id,
       date,
       row_number() over( partition by id order by date desc) rm
FROM dwd_demo_d_i 
GROUP BY id,date 
LIMIT 100;

标准格式 row_number() over()

分组 partition by 字段名称

排序 order by 字段名称 DESC 倒序 ASC 升序

参考网址

Hive 取前后行数据

select 
*, 
lag(rm, 1, 'none') over(partition by id order by date) as last_date 
from
(SELECT id,
       date,
       row_number() over( partition by id order by date desc) rm
FROM dwd_demo_d_i 
GROUP BY id,date 
LIMIT 100) t

lead 向下取值 lag 向上取值

参考网址1

参考网址2

参考文献

1. Hive官方文档 . https://cwiki.apache.org/confluence/display/Hive/
2. lambda-小张 . CSDN . hive的行转列和列转行 . https://blog.csdn.net/m0_55834564/article/details/124045485
3. 小哇666 . CSDN . hive-字符串查找函数 instr和locate . https://blog.csdn.net/qq_41712271/article/details/109221938
4. shining0903lxy . CSDN . hive 的开窗函数row_number() over() . https://blog.csdn.net/weixin_43548518/article/details/104110733
5. TXFSheng . 博客园 . hive求相邻两行数据的差值 . https://www.cnblogs.com/txfsheng/p/9235421.html
6. MachineCYL . CSDN . 【Hive】计算相邻两行时间戳的间隔(lag、lead) . https://blog.csdn.net/u011590738/article/details/125613952

感谢你的观看,YES!