本文首发于数据蚂蚁公众号,原创文章,请勿转载
场景:每天需要发送日报,其中日报中需要对比这周一到昨天(例如:周三)和上周一到上周三的数据,如何让代码每天自动调整获取想要时间段的数据?
问题:怎么知道能字段判定这一周和上一周的的星期一是哪一天
在遇到这个需求时,首先想到了SQL SERVER和MySQL里的datename函数,但写了发现HIVE里不能用,搜索才发现
Hive原生未提供获取一个日期是星期几的方法,不过也也找到了通过hive原生函数来获取星期几的方法。
select column
from Table
left join
where date between get_date(pmod(cast(datediff(get_date(-1),'2017-02-26') as int),7)-7) and get_date(-1)
union all
select column
from Table
left join
where date between get_date(pmod(cast(datediff(get_date(-1),'2017-02-26') as int),7)-14) and get_date(-8)