Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round Timstamp to nearest 15 mins interval in bigquery

I am trying to round the datetime field to nearest 15 mins interval through bigquery standard sql, tried datetime_trunc but it is not allowing to round off to nearest X mins

2018-10-24 01:05:00 to 2018-10-24 01:00:00
2018-10-24 01:08:00 to 2018-10-24 01:15:00
2018-10-24 01:12:00 to 2018-10-24 01:15:00

any other ways to achieve the above conversion in bq standard sql?

Thanks,

like image 405
dineshachanta Avatar asked Oct 28 '18 06:10

dineshachanta


People also ask

What is Date_trunc in BigQuery?

DATE_TRUNC(date_expression, date_part) Description. Truncates a DATE value to the granularity of date_part . The DATE value is always rounded to the beginning of date_part , which can be one of the following: DAY : The day in the Gregorian calendar year that contains the DATE value.

How do I set timezone in BigQuery?

You are right - BigQuery doesn't provide any timestamp conversion methods. In this case, I suggest that you run your GROUP BY based on dimensions of the GMT/UTC timestamp field, and then convert and display the result in the local timezone in your code.

How can I get last day of previous month in BigQuery?

Use LAST_DAY function SELECT LAST_DAY(DATE'2021-05-20', quarter); SELECT LAST_DAY(DATE'2021-05-20', year); Result: 2021-06-30.


1 Answers

Below is for BigQuery Standard SQL (assuming your field is of TIMESTAMP type as it is stated in question title)

TIMESTAMP_SECONDS(900 * DIV(UNIX_SECONDS(dt_from) + 450, 900))   

You can test, play with it using dummy data from your question

#standardSQL
WITH `project.dataset.table` AS (
  SELECT TIMESTAMP '2018-10-24 01:05:00' dt_from UNION ALL
  SELECT '2018-10-24 01:08:00' UNION ALL
  SELECT '2018-10-24 01:12:00'
)
SELECT dt_from, TIMESTAMP_SECONDS(900 * DIV(UNIX_SECONDS(dt_from) + 450, 900)) dt_to
FROM `project.dataset.table`  

with result

Row dt_from                 dt_to    
1   2018-10-24 01:05:00 UTC 2018-10-24 01:00:00 UTC  
2   2018-10-24 01:08:00 UTC 2018-10-24 01:15:00 UTC  
3   2018-10-24 01:12:00 UTC 2018-10-24 01:15:00 UTC    

In case if your field is of DATETIME type (as it stated in the question itself) - you can use below version of above

#standardSQL
WITH `project.dataset.table` AS (
  SELECT DATETIME '2018-10-24 01:05:00' dt_from UNION ALL
  SELECT '2018-10-24 01:08:00' UNION ALL
  SELECT '2018-10-24 01:12:00' 
)
SELECT dt_from, DATETIME(TIMESTAMP_SECONDS(900 * DIV(UNIX_SECONDS(TIMESTAMP(dt_from)) + 450, 900))) dt_to
FROM `project.dataset.table`
like image 188
Mikhail Berlyant Avatar answered Oct 11 '22 10:10

Mikhail Berlyant