Google BigQuery从日期中减去任意数量的工作日
技术问答
171 人阅读
|
0 人回复
|
2023-09-12
|
我试图构建一个可以从日期中减去任何数量的工作日的函数。到目前为止,我想到了这一点
2 d, Y7 E3 C9 SCREATE TEMPORARY FUNCTION working_days_diff(the_date DATE,num_of_days INT64) AS( CASE WHEN EXTRACT(DAYOFWEEK FROM the_date) in (2,3,4,5,6,7) THEN CASE WHEN (EXTRACT(DAYOFWEEK FROM the_date) - num_of_days) > 1 THEN DATE_SUB(the_date,INTERVAL num_of_days DAY) ELSE DATE_SUB(the_date,INTERVAL (num_of_days 2) DAY) END ELSE DATE_SUB(the_date,INTERVAL (num_of_days 1) DAY) END);当相减天数相隔不到一周时,该方法效果良好,如以下情况:
+ p0 X9 |0 g {- l select working_days_diff(DATE("2018-04-12"),3) UNION ALL select working_days_diff(DATE("2018-04-12"),4) UNION ALL select working_days_diff(DATE("2018-04-12"),5) UNION ALL select working_days_diff(DATE("2018-04-12"),6) UNION ALL select working_days_diff(DATE("2018-04-12"),7) UNION ALL select working_days_diff(DATE("2018-04-12"),8)但是,当我想减去的天数增加时,它就会失败。
8 u, e- [( s! _4 b! s select working_days_diff(DATE("2018-04-12"),9) -- this should return "2018-03-30" UNION ALL select working_days_diff(DATE("2018-04-12"),10) -- this should return "2018-03-29" UNION ALL select working_days_diff(DATE("2018-04-12"),20) -- this should return "2018-03-15" - \. m( ?; N& E8 [. C8 J1 C
解决方案: |
|
|
|
|
|