如何在几天内排除两个日期之间的区别,不包括周末?

时间:2018-06-15 11:01:01

标签: google-bigquery

我如何获得两个日期之间的差异,不包括周末?

对于上下文,这是为了在创建和关闭故障单的日期之间进行服务级别协议(SLA)监视。

1 个答案:

答案 0 :(得分:1)

下面的示例适用于BigQuery Standard SQL

#standardSQL
CREATE TEMP FUNCTION CUSTOM_DATE_DIFF(startDate DATE, endDate DATE) AS ((
  SELECT
    1 + DATE_DIFF(endDate, startDate, DAY) -
    2 * DATE_DIFF(endDate, startDate, WEEK) -
    IF(EXTRACT(DAYOFWEEK FROM startDate) = 1, 1, 0) -
    IF(EXTRACT(DAYOFWEEK FROM endDate) = 7, 1, 0)
));
WITH test AS (
  SELECT DATE '2018-05-01' startDate, DATE '2018-05-11' endDate
)
SELECT 
  startDate, 
  endDate, 
  CUSTOM_DATE_DIFF(startDate, endDate) diff
FROM test