1 snowflake-cloud-data-platform
如何在不创建表格的情况下计算雪花中两个日期之间的工作时间?我尝试过像 (datediff) 和时间戳这样的函数,但我无法找到解决方案
我想得到类似的东西
+---------------------+---------------------+---------------+
| create_Task | Solved_Task | BusinessHours |
+---------------------+---------------------+---------------+
| 2012-03-05 09:00:00 | 2012-03-05 15:00:00 | 6.000000 |
| 2012-03-05 10:00:00 | 2012-03-06 10:00:00 | 8.000000 |
| 2012-03-05 11:00:00 | 2012-03-06 10:00:00 | 7.000000 |
| 2012-03-05 10:00:00 | 2012-03-06 15:00:00 | 13.000000 |
| 2012-03-09 16:00:00 | 2012-03-12 10:00:00 | 2.000000 |
| 2012-03-06 16:00:00 | 2012-03-15 10:00:00 | 50.000000 |
| 2012-03-09 16:00:00 | 2012-03-19 10:00:00 | 42.000000 |
+---------------------+---------------------+---------------+
Run Code Online (Sandbox Code Playgroud)
我想指定工作时间,以便我可以计算工作时间
一种方法是创建工作时间表。然后你可以运行一个相当简单的查询:
select
t.id
, sum(datediff(‘second’,
-- calculate the max of the two start time
(case when t.start <=
w.working_day_start_timestamp
then w.working_day_start_timestamp
else t.start
end),
-- calculate the min of the two end times
(case when t.end >=
w.working_day_end_timestamp
then w.working_day_end_timestamp
else t.end
end)
)) / 3600 -- convert to hourly
as working_hour_diff
from
working_days_times w,
cross join time_intervals t
where -- select all intersecting intervals
(
t.start <= w.working_day_end_timestamp
and
t.end >= w.working_day_start_timestamp
)
and -- select only working days
w.is_working_day
group by
t.id
Run Code Online (Sandbox Code Playgroud)
如果需要函数,本文介绍了在雪花中实现一个Javascript UDF:https : //medium.com/dandy-engineering-blog/how-to-calculate-the-number-of-working-hours-between- sql-b5696de66e51 中的两个时间戳
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |