目录
1、分组排序函数:row_number() over()
2、时间戳函数:
3、列转行函数:collect_list/collect_set(列转行)
4、concat_ws()函数:
1、分组排序函数:row_number() over()
语法:row_number() over(partition by 分组列 order by 排序列 desc)
只对salary排序,不分组
select id,name,age,salary,row_number()over(order by salary desc) rn from TEST_ROW_NUMBER_OVER t
分组并排序取第一条数据:对id字段分组按salary降序排序,取salary薪水最多的第一条记录
select * from(select id,name,salary,row_number()over(partition by id order by salary desc) rank from TEST_ROW_NUMBER_OVER t) where rank =1
2、时间戳函数:
时间戳定义:时间戳是数据库常用的存放日期的形式之一,表示从 UTC 时间’1970-01-01 00:00:00’开始到现在的秒数
时间戳定义:时间戳是数据库常用的存放日期的形式之一,表示从 UTC 时间’1970-01-01 00:00:00’开始到现在的秒数
2.1、unix_timestamp()返回当前时间戳
hive> select unix_timestamp();
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
1530241405
Time taken: 0.237 seconds, Fetched: 1 row(s)
hive> select unix_timestamp('2018-06-29 00:00:00');
OK
1530201600
Time taken: 0.232 seconds, Fetched: 1 row(s)
2.2、from_unixtime 函数用法
1、from_unixtime(int/bigint timestamp) 返回 timestamp 时间戳对应的日期,默认格式为 yyyy-MM-dd HH:mm:ss。
hive> select from_unixtime(1000000000); OK 2001-09-09 09:46:40 Time taken: 0.316 seconds, Fetched: 1 row(s)
2、from_unixtime(unix_timestamp(),'yyyy'),获取当前时间戳,并使用from_unixtime()函数指定为YYYY格式。
升级:from_unixtime(unix_timestamp(),'yyyy')||'01',获取当前时间戳,并指定YYYY格式,和字符‘01’拼接,获取到当年一月份时间,如202001,202101。
3、列转行函数:collect_list/collect_set(列转行)
1、collect_set(要转换成行的列):将一个字段(列)里面的内容转换成行,并去重
如:collect_set(timest),红框内的就是使用了collect_set函数之后的呈现效果,这个函数将一个列里面的内容封进一个列表里面,以行的形式呈现,并去重
collect_set()函数详细博客:Hive笔记之collect_list/collect_set(列转行) - CC11001100 - 博客园
还能使用size(collect_set())包裹住collect_set()函数就能求出分组里面的长度
2、collect_list()函数:语法:collect_list(分组列)
将分组中的某列转为一个数组返回,不同的是collect_list不去重而collect_set去重
好处:这两个函数有一个好处就是,不用使用group by就能够实现分组,group by使用后select的列必须是作为分组依据的列,不能随意分组,才能求出count()、sum(),max()等;但是这两个函数却可以做到随意分组。
4、concat_ws()函数:
语法:concat_ws(合并时的分隔符,合并字段1,字段2。。。)
collect_set(字段):根据某个字段分组后,把分在一组的数据合并在一起,默认分隔符','



