在Hive中获取第15个最后工作日日期yyyyMMdd(仅不包括周末)



我有一个带有日期列的表(日期为字符串格式yyyyMMdd(。我的要求是设计一个逻辑,在不使用UDF或shell脚本的情况下,从"日期列值等于前15个工作日的日期"(仅不包括周六和周日(的表中提取数据。例如,今天是2020年2月21日;逻辑应该产生一个输出:20200203。

假设根据您的示例,您实际上指的是前14个工作日,并且您忽略了假日,那么它只是一个date_sub函数,其中包含一周中某一天的case语句。

case from_unixtime(unix_timestamp(event_date,'yyyyMMdd'),'u')
when 1 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),20),'-','')
when 2 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),20),'-','')
when 3 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),20),'-','')
when 4 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),20),'-','')
when 5 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),18),'-','')
when 6 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),18),'-','')
when 7 then regexp_replace(date_sub(from_unixtime(unix_timestamp(event_dt,'yyyymmdd' )),19),'-','')
end as new_date

这假设周六/周日应该像周一一样对待,如果周六/周日应该像周五那样,那么就用19、20。

如果你需要考虑假期,那么你需要创建一个包含每一天的日历表,并注意哪些日子是假期,然后它是表的一个连接,如果是这样的话,可以找到更多的逻辑。