英文:
SQL (BigQuery) timediff in minutes between 2 timestamp excluding week-end and only from 5 A.M to 6 P.M
问题
我对SQL还很陌生,不确定是否可以在BigQuery上创建此查询。我有两列,都是以timestamp
格式,例如2023-02-03 08:54:05 UTC
。第一列名为start
,第二列名为end
。我需要找到这两列之间的时间差以分钟
计算,但是:
-
仅计算从上午
5:00
到下午6:00
之间的时间差。例如,如果start
是2023-01-02
下午5:58
,而end
是2023-01-03
上午5:01
,结果应为3
分钟。 -
排除周六和周日的计算。
在BigQuery中是否有可能实现这个要求?我查看了Google Cloud的文档,但没有找到如何排除特定时间范围的相关信息。我知道有TIME_DIFF()
函数,但似乎没有办法排除一段时间。
我是否应该创建一个包含未来几年内所有周六和周日日期的表,以帮助我进行这个查询?
英文:
I am quite new to SQL ad I am not sure if this is even possible to create this quid of query on Bigquery. I have 2 columns both in a timestamp
format such as 2023-02-03 08:54:05 UTC
. The first column is called start
and the second end
. I need to find the timediff
between those 2 colons in minutes
but:
-
count only the difference from
hours
between5:00 A.M
to6:00 P.M
. As an exemple, ifstart
is2023-01-02
at5:58 P.M
andend
is2023-01-03
at5:01 A.M
the result should be 3 minutes. -
exclude saturday and sunday from calculations
Is this possible using BigQuery? I have not seen any documentation to exclude certain time ranges in their time functions documentation on Google Cloud. I know TIME_DIFF()
but seems there is no way to exclude a range of hours.
Should I create a table with the list of all saturday and sunday as dates during the next couple of years to help me with this query?
答案1
得分: 0
以下是翻译的部分:
这里是翻译结果:
首先,我们计算从开始到结束的时间差。
然后,我们计算第一天和最后一天的分钟数,并将这些数字相加。如果这个数字大于从开始到结束的时间差,我们就使用该值。
接下来,我们计算开始和结束之间的天数,并减去一天,因为我们已经计算了第一天和最后一天。我们忽略负值,并用零代替。然后,我们乘以每天的工作分钟数。
由于您不希望包括周末,我从这里的查询中获取了相关内容(https://stackoverflow.com/questions/55956297/date-diff-but-only-counting-business-days)。请您也查看一下这个链接。
请注意,该查询并未检查开始和结束时间是否在工作日的上午5点到下午6点之间。而且,公共假期也没有从计算中排除。
英文:
Here you go:
with tbl as (Select datetime "2023-01-02T17:58:00.000" as start, datetime"2023-01-03T05:01:00.000" as ending)
select * ,
timestamp_diff( ending, start, minute ) as total_minutes,
LEAST(datetime_diff( ending, start, minute ) , # case same date
datetime_diff( datetime_add(datetime_trunc(start,day),interval 6+12 hour) , start, minute ) + # minutes of 1st day
datetime_diff( ending, datetime_add(datetime_trunc(ending,day),interval 5 hour) , minute ) # minutes of last day
)+
GREATEST(0,
#date_diff(date(ending),date(start),day) # This would count also the weekends, but this excludes the weekends
case
when date_diff(date(ending), date(start), week) > 0
then date_diff(date(ending), date(start), day) - (date_diff(date(ending), date(start), week) * 2)
else
date_diff(date(ending), date(start), day)
end
-1)*12*60 # 12 working hours times 60 minutes
as minutes_between_5am_and_6pm
from tbl
First we calculate the difference from start to end.
Then we calculate the minutes of the first day and the minutes of the last day. We add these numbers together.
If this number is greater than from start to end, we use that value instead.
Next we calculate the days between start and end and substract one day, because we already have the first and last day calculated. We ignore negative values and use zero instead. Then we multiply with the working minutes per day.
Since you do not want to have weekends included, I took the query from here. Please have a look on this as well.
Please take into account, that the query does not check that the start and end is between 5 am and 6 pm on a working day. Also public holidays are not excluded from the calculation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论