- 1、连续n天
例如连续12登陆,先日期进行从小到大进行排序,再rank ,然后日期减去rank的序号,有多少个相同的连续值就是连续多少天
- 2、数据只有本月和本月数添加第三列是之前12个月的数总和
(sum(ct2.CREATE_PROJECT_CURRENT_MONTH_CNT) over(ORDER BY ct2.CURRENT_MONTH_ID ASC ROWS BETWEEN 12 preceding AND 1 preceding)
如果原始数据有缺失月份可以先进行缺失月份的补齐默认值补0
- 3、数据只有本月和本月数添加第三列是上个月数第四列上年同月数
使用left join 配合case when 使用 灵活填充
- 4、数据只有本月和本月数添加第三列是本年截止当前数总和
通过年 来进行分组
- 5 、列转行 行转列
concat
concat_ws
collect_set
collect_list
lateral view explode(集合)
lateral view explode(split(order_value,','))
- 6、数据类型转换
cast(xxx as xxx)
- 7、case when 灵活方式使用
- 8、脱敏
regexp_replace(selphone,substr(selphone,4,4),'****')
-9、自定义函数
## 临时函数使用
进入hive的交互shell中
1. 上传自定义udf的jar
add jar /path/to/lower.jar
2. 创建临时函数
create temporary function xxoo_lower as 'test.ql.LowerUDF';
3. 验证
select xxoo_lower("Hello World!");
## 永久函数使用
1. 把自定义函数的jar上传到hdfs中.
hdfs dfs -put lower.jar 'hdfs:///path/to/hive_func';
2. 创建永久函数
create function TimeDiff as 'yang.udf.TimeDiff' using jar 'hdfs:///hive-function/hive-function-1.0-SNAPSHOT.jar';
3. 验证
select TimeDiff('2019-09-09','2019-09-10','yyyy-MM-dd');
show functions;
- 10、sql执行顺序
from
on
join
where
group by
having
select
distinct
over
order by
limit
- 11 union 多张临时表
with
temp1 as (),
temp2 as(),
···
select from temp1 union temp2 ···
- 12 、数据清洗
去除mysql中倒过来的因为中文输入法,字段包含hive默认的分隔符\u0000-\u0013
regexp_replace(m.changed_way,'([\\u0000-\\u0013]+)','')
-13 hive sql中使用正则表达式
set hive.support.quoted.identifiers=None;
select a.pin, `(pin)?+.+` from Table
-14 优化排序
order by全局排序,一个reduce实现,不能并行故效率偏低
sort by部分有序,配合distribute by使用
cluster by col1 == distribute by col1 sort by col1,但不能指定排序规则
- 15 join 优化
多表join的key值统一则可以归为一个reduce;
先过滤后join;
小表在前读入内存,大表在后;
-16 十三位时间戳格式转化
from_unixtime(cast(substr(time_stamp,1,10) as bigint), 'yyyy-MM-dd')
来源:CSDN
作者:yagch
链接:https://blog.csdn.net/qq_34897849/article/details/103936592