I am trying to generate an array from end_date_time and start_date_time and from that array extracting hours like for eg. 2024-03-09 12:00:18.000000 UTC (start_date_time) and 2024-03-09 15:00:18.000000 UTC (end_date_time) hours should be 12,13,14,15.
(There will be different segments and event_type as well)
Now I want to group these hours and count them. Here's my sample data:
My Output should look like below: Output from sample data
I tried below query but not getting desired results.
with q1 as (
Select segment, event_type,hours from
`id.dataset.my_tab`,
unnest(generate_timestamp_array(end_date_time,start_date_time, interval 1 hour)) as hours
),
q2 as (
select segment, event_type,
EXTRACT(HOUR FROM hours) as hours_category from q1
)
Select segment, event_type, hours_category,
count(hour_category) as count_hours
from q2
Group by hour_category, event_type,segment