数据仓库 业务分析常用函数

一 collect_set函数,将某个字段形成一个数组

select course, collect_set(area), avg(score) from stud group by course;
chinese ["sh","bj"]     79.0
math    ["bj"]  93.5

还可以通过下标取值

select course, collect_set(area)[0], avg(score) from stud group by course;
chinese sh      79.0
math    bj      93.5

二 日期处理函数

1 date_format函数(根据格式整理日期),分析一些月度指标时,将不同日的时间,截取出同样的年月。

select date_format('2020-06-26','yyyy-MM');
OK
_c0
2020-06

2 date_add和date_sub,日期加减,加减天数可以为负。

select date_add('2020-06-25',5);
OK
_c0
2020-06-30
select date_sub('2020-06-25',5);
OK
_c0
2020-06-20

3 next_day函数

取当前天的下一个周一

select next_day('2020-06-26','MO');
OK
_c0
2020-06-29

取当前周的周一,注意,这个方法只能取当前周的周一,其他星期数则不能。因为下一个周一一定在下周,再减去7刚好是本周周一。但如果今天是周五,则下一个周六还在本周,不能减7。

select date_add(next_day('2020-06-26','MO'),-7);
OK
_c0
2020-06-22

4 last_day函数,得到本月的最后一天的日期

select last_day('2020-06-26');
OK
_c0
2020-06-30

猜你喜欢

转载自www.cnblogs.com/noyouth/p/13195543.html