Amazon redshift 或 Athena/presto 工作日计算介于 2 天之间

Amazon redshift or Athena/presto business day calculation between 2 days

我有一个日历 table 但我不确定如何使用 amazon redshift 或 amazon athena(presto) 减去工作日。我看过一些提到使用日历但无法使用 redshift 或 athena 的帖子。任何帮助,将不胜感激。这是我到目前为止所拥有的。它是我在工作日需要的 2 个不同 table 之间的减法。谢谢

table1.createddate (start date)
table2.createddate (end date)
cal.date (cal table, column 'date' is what i can inner join with)
cal.is_week_end is either yes or no on this table.
SELECT table1.id,
table1.createddate,
table2.NAME,
table2.createddate,
Datediff(day, table1.createddate, table2.createddate) AS age1
FROM   table2
LEFT JOIN table1
ON table2.id = table1.id2;

如果表有与其匹配的 ID,那么您可以使用相关子查询。我认为这会起作用:

select t1.*, t2.*,     -- whatever columns you want
       (select sum( (c.is_weekend = 'no')::int )
        from calendar c
        where c.date >= t1.createddate and
              c.date <= t2.createddate
       ) as num_nonweekend_days
from table1 t1 join
     table2 t2
     on t1.id = t2.id;

如果关联子句有问题(有时会发生),您还可以使用:

select t1.id,
       count(c.date) as num_nonweekend_days
from table1 t1 join
     table2 t2
     on t1.id = t2.id left join
     calendar c
     on c.date >= t1.createddate and
        c.date <= t2.createddate and
        c.is_week_end = 'no'
group by t1.id;

如果您需要额外的列,您需要将它们包含在 selectgroup by 中。