首页 > 解决方案 > 如何找到总工作日或总工作时间?

问题描述

我正在寻求帮助以查找两个日期之间花费的“总工作日”和“总工作时间”;开始日期和结束日期。到目前为止,我只能找到两个日期之间的总小时数,不包括周末。然后我将这个总小时数转换为天数(除以 24)。

今后,我需要将公共假期(如感恩节、圣诞节等)纳入其中。另外,我如何找到总工作时间而不是工作日数(假设工作时间是上午 8 点到下午 5 点)?

我的导师给了我一个“作业”,使用“序列”来查找总工作日数,但我似乎找不到。我也不确定“序列”是要使用的实际语法还是只是在线搜索的关键字。

总之,需要您的指导才能找到以下内容:

我的编码如下:

(DATE_DIFF('hour', start_date, end_date) - (DATE_DIFF('week',start_date, end_date)*2 * 24) - ((CASE WHEN DAY_OF_WEEK(start_date) > 5 THEN 1 ELSE 0 END) * 24) - ((CASE WHEN DAY_OF_WEEK(end_date) > 5 THEN 1 ELSE 0 END) * 24)) / 24.0 AS workday_spent

谢谢你的帮助!

标签: sqlpresto

解决方案


在这里搜索sequencehttps ://prestodb.io/docs/current/functions/array.html

这是对我有用的查询:

select count(*) as working_days, count(*)*8 as working_hours
from unnest(sequence(start_date, end_date)) t(d) 
where day_of_week(d) not in (6, 7);

以及公共假期:

select count(*) as working_days, count(*)*8 as working_hours
from unnest(sequence(start_date, end_date)) t(d) 
where day_of_week(d) not in (6, 7);
  and d not in (select d from public_holidays);

实际查询:

presto> select count(*) from unnest(sequence(cast('2020-01-01' as date), cast('2020-12-31' as date))) t(d) where day_of_week(d) not in (6, 7);
 _col0
-------
   262
(1 row)

推荐阅读