sql - 如何找到总工作日或总工作时间?
问题描述
我正在寻求帮助以查找两个日期之间花费的“总工作日”和“总工作时间”;开始日期和结束日期。到目前为止,我只能找到两个日期之间的总小时数,不包括周末。然后我将这个总小时数转换为天数(除以 24)。
今后,我需要将公共假期(如感恩节、圣诞节等)纳入其中。另外,我如何找到总工作时间而不是工作日数(假设工作时间是上午 8 点到下午 5 点)?
我的导师给了我一个“作业”,使用“序列”来查找总工作日数,但我似乎找不到。我也不确定“序列”是要使用的实际语法还是只是在线搜索的关键字。
总之,需要您的指导才能找到以下内容:
在不使用 date_diff 的情况下查找总工作日花费
查找总工作时间
如何纳入公共假期
我的编码如下:
(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
谢谢你的帮助!
解决方案
在这里搜索sequence
:https ://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)
推荐阅读
- generics - 通用 JSON 反序列化
- javascript - 使用原型继承与委托模式
- excel - 对象变量或未设置块 - 用户窗体
- .htaccess - 如何从 htaccess 中的 HTTP_REFERER 获取域
- visual-studio-code - 在 Visual Studio Code 中,我可以在 tasks.json 中引用 c_cpp_properties.json 中的配置元素吗?
- php - 在 WordPress 上使用复制器插件安装克隆站点时,为什么会出现安装错误,指出“wp-config.php 为空”?
- java - 如何在 Java 中向下转换数组?(多态性)
- node.js - 如何更新 MongoDB 嵌套对象中的文档数据?
- python - numpy 浮点精度差
- angular - 告诉我如何以角度显示 json 对象值?