sqlvertica

SQL VERTICA group by intervals


How to group records by interval 4 hours?

For example, I have rows that contain data about events durations.

event_id start_date end_date
1 2024-08-16 14:30:00 2024-08-16 16:00:00
1 2024-08-16 16:00:00 2024-08-16 17:30:00
1 2024-08-16 17:30:00 2024-08-16 19:00:00
1 2024-08-16 19:00:00 2024-08-16 20:30:00
1 2024-08-16 20:30:00 2024-08-16 22:00:00
1 2024-08-16 22:00:00 2024-08-16 23:30:00

I want to group them but max duration of 1 row should be 4 hours.

Result:

event_id start_date end_date
1 2024-08-16 14:30:00 2024-08-16 17:30:00
1 2024-08-16 17:30:00 2024-08-16 20:30:00
1 2024-08-16 20:30:00 2024-08-16 23:30:00

I tried to use something like this to display an increased duration but no idea how to use it for grouping

select sum(end_date - start_date) over (partition by event_id order by start_date)

Solution

  • WITH grouping AS (
        SELECT 
            event_id, 
            start_date, 
            end_date,
            MIN(start_date) OVER (PARTITION BY event_id) AS first_start_date,
            FLOOR(
                TIMESTAMPDIFF('second', MIN(start_date) OVER (PARTITION BY event_id), start_date) 
                / (4 * 3600)
            ) AS group_num
        FROM event
    )
    SELECT
        event_id,
        MIN(start_date) AS start_date,
        MAX(end_date) AS end_date
    FROM grouping
    GROUP BY event_id, group_num
    ORDER BY event_id, start_date;