数据仓库常用建模方法
2019年2月22日大约 1 分钟
常用函数
get_json_object(data,'$.[name]')
获取data中json格式名为name的值, 示例如下:
select get_json_object('{"aaa":"xxxx"}',"$.aaa")
-- 结果为 xxxx
regexp_replace(str,old,new)
将str字符串中所有的 old 替换成 new, 示例如下:
select regexp_replace('fooler','oo|er','xx')
-- 结果为 fxxlxx
substr(str,start,[..len])
将str字符串中截取从start开始的 len长字符串instr(string str, string substr)
str字符串中查找substr,存在则返回substr在字符串中的位置round(num, count)
返回num的小数点后count位的小数, 示例如下:
select round(0.12345678,3)
-- 结果为 0.123
date_sub(date,day)
返回 date 之前的 day 天的日期, 示例如下:
select date_sub('2019-01-01',1)
-- 结果为 2018-12-31
floor(num)
返回 num数字向下取整的整数值
select floor(2.7)
-- 结果为 2
collect_set()/collect_list() group by
列转行lateral view explode() xxx as xxxx
行转列row_number() over(partioner by xxx order by xxx [desc] )
分组排序 (顺序计算排名)rank()over(partition by name order by sal desc )
分组排序 (排名相同时会重复,总数不会变)dense_rank() over(partition by name order by sal desc )
分组排序 (排序相同时会重复,总数会减小)cast(xx as [type])
类型转换from_unixtime(xxx,[format])
时间戳转 timestampunix_timestamp(xxx,[format])
timestamp 转时间戳sum(xxx) over(order by xxx rows between unbounded preceding and current row)
累加和计算
原创声明
平台文章均为原创文章,未经许可,禁止转载。
如需转载,请联系作者获取授权,并注明来源及原文链接。