I have table like the following:
person | Session | session_start | session_end | half_hour_start | half_hour_end |
---|---|---|---|---|---|
A | A001 | 9/13/2020 7:58:00 PM | 9/13/2020 8:10:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM |
A | A002 | 9/13/2020 8:02:00 PM | 9/13/2020 8:13:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM |
A | A003 | 9/13/2020 8:27:00 PM | 9/13/2020 8:28:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM |
B | B001 | 9/13/2020 8:20:00 PM | 9/13/2020 8:28:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM |
B | B002 | 9/13/2020 8:28:00 PM | 9/13/2020 8:43:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM |
The goal is to count distinct minutes per every person for all session within 30 min block (half_hour_start - half_hour_end). Count starts from minute 00, ends by minute 29 (so there're 30 distinct minutes in total).
So that even in case if a person had session starting at 9/13/2020 8:00:01 PM and ending at 9/13/2020 8:00:05 PM, this person will still get credit for 1 minute - minute '00'. We're interested not in count of full minutes, but in count of all distinct minutes where session had place, even partially.
I need to get results like:
---old version---
person | distinct_minutes_count |
---|---|
A | 14 |
B | 10 |
---new version---
person | distinct_minutes_count |
---|---|
A | 16 |
B | 10 |
(which could be coming from:
person | Session | session_start | session_end | half_hour_start | half_hour_end | distinct_minutes_count_per_person |
---|---|---|---|---|---|---|
A | A001 | 9/13/2020 7:58:00 PM | 9/13/2020 8:10:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 16 |
A | A002 | 9/13/2020 8:02:00 PM | 9/13/2020 8:13:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 16 |
A | A003 | 9/13/2020 8:27:00 PM | 9/13/2020 8:28:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 16 |
B | B001 | 9/13/2020 8:20:00 PM | 9/13/2020 8:28:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 10 |
B | B002 | 9/13/2020 8:28:00 PM | 9/13/2020 8:43:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 10 |
)
The intermediate steps needed, probably, are:
person | Session | session_start | session_end | half_hour_start | half_hour_end | distinct_minute_per_session | distinct_minutes_count_per_session | distinct_minute_per_person | distinct_minutes_count |
---|---|---|---|---|---|---|---|---|---|
A | A001 | 9/13/2020 7:58:00 PM | 9/13/2020 8:10:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 00,01,02,03,04,05,06,07,08,09,10 | 11 | 00,01,02,03,04,05,06,07,08,09,10,11,12,13,27,28 | 16 |
A | A002 | 9/13/2020 8:02:00 PM | 9/13/2020 8:13:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 02,03,04,05,06,07,08,09,10,11,12,13 | 12 | 00,01,02,03,04,05,06,07,08,09,10,11,12,13,27,28 | 16 |
A | A003 | 9/13/2020 8:27:00 PM | 9/13/2020 8:28:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 27,28 | 2 | 00,01,02,03,04,05,06,07,08,09,10,11,12,13,27,28 | 16 |
B | B001 | 9/13/2020 8:20:00 PM | 9/13/2020 8:28:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 20,21,22,23,24,25,26,27,28 | 9 | 20,21,22,23,24,25,26,27,28,29 | 10 |
B | B002 | 9/13/2020 8:28:00 PM | 9/13/2020 8:43:00 PM | 9/13/2020 8:00:00 PM | 9/13/2020 8:30:00 PM | 28,29 | 2 | 20,21,22,23,24,25,26,27,28,29 | 10 |
But I don't see options of creating list values for a column in Snowflake.