SQL (BigQuery) timediff in minutes between 2 timestamp excluding week-end and only from 5 A.M to 6 P.M

huangapple go评论67阅读模式
英文:

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。我需要找到这两列之间的时间差以分钟计算,但是:

  1. 仅计算从上午5:00到下午6:00之间的时间差。例如,如果start2023-01-02下午5:58,而end2023-01-03上午5:01,结果应为3分钟。

  2. 排除周六和周日的计算。

在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:

  1. count only the difference from hours between 5:00 A.M to 6:00 P.M. As an exemple, if start is2023-01-02at5:58 P.Mand end is 2023-01-03 at 5:01 A.M the result should be 3 minutes.

  2. 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.

huangapple
  • 本文由 发表于 2023年3月7日 16:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定