I have a jsonb field in a database table that holds the following data:
{"access": {
"D2024.06.13": [{"qty": 1, "time": "12:05"}, {"qty": 3, "time": "12:32"}],
"D2024.06.14": [{"qty": 1, "time": "08:37"}]
}}
I would like to group the data by date and by time in 10 min slots (00:00 to 23:50)
so
2024.06.13
12:00 : 1
12:10 : 0
12:20 : 0
12:30 : 3
12:40 : 0
...
{'2024.06.13' : {'12:00':1, '12:10':0, '12:20':0, '12:30':3, '12:40':0 ....}, '2024.06.13' : {'12:00':1 ....}
so I can draw a timeline afterwards.
2
Answers
In multiple steps:
MOTE: The selected days always start at ’00:00′, so you might need to do some tweaking when you whish them to start at ’12:05′ on your first day, but I leave that to you 😉
see: DBFIDDLE
Here is it using CTEs.
flat_data
CTE)time_slots
CTE)Demo