我如何获得两个日期之间的差异,不包括周末?
对于上下文,这是为了在创建和关闭故障单的日期之间进行服务级别协议(SLA)监视。
答案 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