Skip to content
Advertisement

SQL (BigQuery) Grouping Runtime Per Day

I have the following data which I want to group into seconds per day in BigQuery.
Source Table:

+--------------+---------------------+---------------------+
| ComputerName | StartDatetime       | EndDatetime         |
+--------------+---------------------+---------------------+
| Computer1    | 2020-06-10T21:01:28 | 2020-06-10T21:20:19 |
+--------------+---------------------+---------------------+
| Computer1    | 2020-06-10T22:54:01 | 2020-06-11T05:21:48 |
+--------------+---------------------+---------------------+
| Computer2    | 2020-06-08T09:11:54 | 2020-06-10T11:36:27 |
+--------------+---------------------+---------------------+

I want to be able to visualise the data in the following way

+------------+--------------+------------------+
| Date       | ComputerName | Runtime(Seconds) |
+------------+--------------+------------------+
| 2020-10-10 | Computer1    | 5089             |
+------------+--------------+------------------+
| 2020-10-11 | Computer1    | 19308            |
+------------+--------------+------------------+
| 2020-10-08 | Computer2    | 53285            |
+------------+--------------+------------------+
| 2020-10-09 | Computer2    | 86400            |
+------------+--------------+------------------+
| 2020-10-10 | Computer2    | 41787            |
+------------+--------------+------------------+

I am not too sure of the way I should approach this. Some input would be greatly appreciated.

Advertisement

Answer

This is an interval overlap problem. You can solve this by splitting each time period into separate days and then looking at the overlap for each day:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement