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

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

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,table2.createddate) AS age1
FROM   table2
LEFT JOIN table1
ON table2.id = table1.id2;
qw12c 回答:Amazon redshift 或 Athena/presto 工作日计算 2 天之间

如果表具有匹配它们的 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 中。

本文链接:https://www.f2er.com/4511.html

大家都在问