0

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

4

1 回答 1

0

假设您实际上是指基于您的示例的前第 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。

如果您需要考虑假期,那么您需要创建一个包含每一天的日历表,并注意哪些日子是假期,然后它是对表的连接,如果这是可以弄清楚的更多逻辑案子。

于 2020-02-21T16:22:33.763 回答