繁体   English   中英

在 BiqQuery 中选择上一个和下一个工作日

[英]Select Previous and Next Business Day in BiqQuery

我有一个表“us_holidays_list”,表中有以下数据:

date_id 描述
2022 年 1 月 1 日 元旦
2022 年 1 月 17 日 马丁路德金纪念日
2022 年 2 月 21 日 总统日
2022 年 5 月 30 日 纪念日
2022 年 6 月 19 日 六月节
2022 年 6 月 20 日 六月节(观察到)
2022 年 7 月 4 日 独立日
2022 年 9 月 5 日 劳动节
2022 年 10 月 10 日 哥伦布日
2022 年 11 月 11 日 退伍军人节
2022 年 11 月 24 日 感恩节
2022 年 12 月 25 日 圣诞节

现在在一个 select 语句中,我们需要通过查看我们从 Big query 中的以下查询中获得的日期值来选择上一个和下一个工作日

select date_sub(boms, interval 0 day) as start_date
from unnest(generate_date_array('2020-01-01', current_date(), interval 1 day)) boms

考虑以下方法

with us_holidays_list as (
  select date '2022-1-1' date_id, "New Year's Day" description union all
  select '2022-1-17', "Martin Luther King Day" union all
  select '2022-2-21', "Presidents Day" union all
  select '2022-5-30', "Memorial Day" union all
  select '2022-6-19', "Juneteenth" union all
  select '2022-6-20', "Juneteenth (observed)" union all
  select '2022-7-4', "Independence Day" union all
  select '2022-9-5', "Labor Day" union all
  select '2022-10-10', "Columbus Day" union all
  select '2022-11-11', "Veterans Day" union all
  select '2022-11-24', "Thanksgiving Day" union all
  select '2022-12-25', "Christmas Day" 
), calendar as (
  select day, not extract(dayofweek from day) in (1,7) isBusinessDay
  from unnest(generate_date_array('2021-12-30', current_date())) day
)
select * from (
  select date_id, description, 
    last_value(qualified_day ignore nulls) over(order by day rows between unbounded preceding and 1 preceding) Previous_Business_Day,
    first_value(qualified_day ignore nulls) over(order by day rows between 1 following and unbounded following) Next_Business_Day
  from (
    select *, if(isBusinessDay and date_id is null, day, null) as qualified_day from calendar
    left join us_holidays_list 
    on date_id = day
    where isBusinessDay or not date_id is null
  )
)
where not date_id is null          

带输出

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM