在 SQL presto 中使用 contains 数组函数的问题

问题描述 投票:0回答:1

尝试让以下 SQL Presto 代码正常工作,以便我可以执行工作日计算。如果我写“d -> day_of_week(d) not in (6,7)”,它会起作用,但我还需要从日历中过滤掉假期,因此“and contains(array_agg(date), d)”子句不会工作。 我也尝试过 contains(date, d) 但这也失败了。请指教。

WITH dataset(start_date, end_date) AS (
 values     (date '2021-10-01', date '2021-10-05'),
    (date '2021-10-01', date '2021-10-03'),
    (date '2021-10-02', date '2021-10-10'),
    (date '2021-10-02', date '2021-10-08'),
    (date '2021-10-02', date '2021-10-05')
),
holidays (row, date) AS (
    values (1,date '2021-10-08'))

select start_date,
       end_date,
       cardinality(filter(
               sequence(start_date, end_date, interval '1' day),
                d -> day_of_week(d) not in (6,7) and not contains(array_agg(date), d) 
           )) business_days
from dataset, holidays
sql amazon-athena presto trino
1个回答
0
投票

您可以展平数据、过滤,然后将其聚合回来:

-- sample data
WITH dataset(start_date, end_date) AS (
 values     (date '2021-10-01', date '2021-10-05'),
    (date '2021-10-01', date '2021-10-03'),
    (date '2021-10-02', date '2021-10-10'),
    (date '2021-10-02', date '2021-10-08'),
    (date '2021-10-02', date '2021-10-05')
),
holidays (row, date) AS (
    values (1,date '2021-10-08')),
-- query
with_ids as (
    select start_date,
           end_date,
           row_number() over () rn
    from dataset
)

select arbitrary(start_date) start_date, 
    arbitrary(end_date) end_date, 
    array_agg(day) business_days
from with_ids, 
    unnest(sequence(start_date, end_date, interval '1' day)) as t(day)
where day not in (select date from holidays)
group by rn;

输出:

开始日期 结束日期 工作日
2021-10-01 2021-10-05 [2021-10-01, 2021-10-02, 2021-10-03, 2021-10-04, 2021-10-05]
2021-10-02 2021-10-10 [2021-10-02, 2021-10-03, 2021-10-04, 2021-10-05, 2021-10-06, 2021-10-07, 2021-10-09, 2021-10-10]
2021-10-02 2021-10-08 [2021-10-02, 2021-10-03, 2021-10-04, 2021-10-05, 2021-10-06, 2021-10-07]
2021-10-02 2021-10-05 [2021-10-02, 2021-10-03, 2021-10-04, 2021-10-05]
2021-10-01 2021-10-03 [2021-10-01, 2021-10-02, 2021-10-03]

这里唯一的问题是,这将导致删除没有工作日的范围(您可以通过左连接原始数据将它们恢复)。

© www.soinside.com 2019 - 2024. All rights reserved.