我有一个相当简单的查询;看起来像这样:
SELECT
order_date,
pickup_date,
DATE_DIFF(pickup_date,order_date, day) order_to_pickup
FROM
`orders.table`
唯一的问题是,我需要计算营业日中的日期差,而不是整天。
所以不是上面的查询返回:
+------------+-------------+-----------------+
| order_date | pickup_date | order_to_pickup |
+------------+-------------+-----------------+
| 3/29/19 | 4/3/19 | 5 |
| 3/29/19 | 4/2/19 | 4 |
+------------+-------------+-----------------+
我想它返回:
+------------+-------------+-----------------+
| order_date | pickup_date | order_to_pickup |
+------------+-------------+-----------------+
| 3/29/19 | 4/3/19 | 2 |
| 3/29/19 | 4/2/19 | 3 |
+------------+-------------+-----------------+
答案 0 :(得分:3)
如果您考虑两个日期之间的星期数,我认为有一个聪明的解决方案,但是与此同时,这是一种蛮力的方法:
CREATE TEMP FUNCTION BusinessDateDiff(start_date DATE, end_date DATE) AS (
(SELECT COUNTIF(MOD(EXTRACT(DAYOFWEEK FROM date), 7) > 1)
FROM UNNEST(GENERATE_DATE_ARRAY(
start_date, DATE_SUB(end_date, INTERVAL 1 DAY))) AS date)
);
您的输入,我得到:
CREATE TEMP FUNCTION BusinessDateDiff(start_date DATE, end_date DATE) AS (
(SELECT COUNTIF(MOD(EXTRACT(DAYOFWEEK FROM date), 7) > 1)
FROM UNNEST(GENERATE_DATE_ARRAY(
start_date, DATE_SUB(end_date, INTERVAL 1 DAY))) AS date)
);
WITH OrdersTable AS (
SELECT
DATE '2019-03-29' AS order_date,
DATE '2019-04-03' AS pickup_date UNION ALL
SELECT
'2019-03-29',
'2019-04-02'
)
SELECT
order_date,
pickup_date,
BusinessDateDiff(order_date, pickup_date) AS order_to_pickup
FROM OrdersTable
ORDER BY pickup_date
+------------+-------------+-----------------+
| order_date | pickup_date | order_to_pickup |
+------------+-------------+-----------------+
| 2019-03-29 | 2019-04-02 | 2 |
| 2019-03-29 | 2019-04-03 | 3 |
+------------+-------------+-----------------+
答案 1 :(得分:1)
这里是一种工作方法,用于根据Looker话语社区here中的工作来计算日期之间的工作日。原始示例适用于Redshift,因此我在下面将其改编为BigQuery。
SELECT
CAST(-1*(DATE_DIFF(DATE '2019-01-01', DATE '2019-01-31', DAY) - ((FLOOR(DATE_DIFF(DATE '2019-01-01', DATE '2019-01-31', DAY) / 7) * 2) +
CASE
WHEN EXTRACT(DAYOFWEEK FROM DATE '2019-01-01') - EXTRACT(DAYOFWEEK FROM DATE '2019-01-31') IN (1, 2, 3, 4, 5) AND EXTRACT(DAYOFWEEK FROM DATE '2019-01-31') != 0 THEN 2
ELSE 0
END +
CASE
WHEN EXTRACT(DAYOFWEEK FROM DATE '2019-01-01') != 0 AND EXTRACT(DAYOFWEEK FROM DATE '2019-01-31') = 0 THEN 1
ELSE 0
END +
CASE
WHEN EXTRACT(DAYOFWEEK FROM DATE '2019-01-01') = 0 AND EXTRACT(DAYOFWEEK FROM DATE '2019-01-31') != 0 THEN 1
ELSE 0 END)) AS int64) AS weekdays
将其应用于您的数据集,我们得到:
SELECT
order_date,
pickup_date,
CAST(-1*(DATE_DIFF(order_date, pickup_date, DAY) - ((FLOOR(DATE_DIFF(order_date, pickup_date, DAY) / 7) * 2) +
CASE
WHEN EXTRACT(DAYOFWEEK FROM order_date) - EXTRACT(DAYOFWEEK FROM pickup_date) IN (1, 2, 3, 4, 5) AND EXTRACT(DAYOFWEEK FROM pickup_date) != 0 THEN 2
ELSE 0
END +
CASE
WHEN EXTRACT(DAYOFWEEK FROM order_date') != 0 AND EXTRACT(DAYOFWEEK FROM pickup_date) = 0 THEN 1
ELSE 0
END +
CASE
WHEN EXTRACT(DAYOFWEEK FROM order_date) = 0 AND EXTRACT(DAYOFWEEK FROM pickup_date) != 0 THEN 1
ELSE 0 END)) AS int64) AS weekdays
FROM
`orders.table`
答案 2 :(得分:1)
这应该是@Elliott Brossard提到的简化的非暴力解决方案:
select
order_date,
pickup_date,
case
when date_diff(pickup_date, order_date, week) > 0
then date_diff(pickup_date, order_date, day) - (date_diff(pickup_date, order_date, week) * 2)
else
date_diff(pickup_date, order_date, day)
end
from `orders.table`
答案 3 :(得分:0)
另一种解决方案。删除周末天数:
SELECT
order_date,
pickup_date,
date_diff(pickup_date, order_date, day)
- date_diff(pickup_date, order_date, WEEK(SUNDAY))
- date_diff(pickup_date, order_date, WEEK(SATURDAY)) as order_to_pickup
FROM `orders.table`