I have a department table that has a jsonb field that give the hours of operation that looks like:
{
"Friday": {
"closeTime": "1970-01-01T17:00:00.000Z",
"isOpen": true,
"isOpen24Hours": false,
"openTime": "1970-01-01T08:00:00.000Z"
},
"Monday": {
"isOpen": false
},
"Saturday": {
"isOpen": false
},
"Sunday": {
"isOpen": false
},
"Thursday": {
"closeTime": "1970-01-01T17:00:00.000Z",
"isOpen": true,
"isOpen24Hours": false,
"openTime": "1970-01-01T08:00:00.000Z"
},
"Tuesday": {
"closeTime": "1970-01-01T17:00:00.000Z",
"isOpen": true,
"isOpen24Hours": false,
"openTime": "1970-01-01T08:00:00.000Z"
},
"Wednesday": {
"closeTime": "1970-01-01T17:00:00.000Z",
"isOpen": true,
"isOpen24Hours": false,
"openTime": "1970-01-01T08:00:00.000Z"
}
}
I have another table that associates some activity on a day with a department and I want to know if on the day that the activity happened is the department was open. So how do I query this jsonb field with the day of week of the date from the other table?
I have tried something like:
select a.activity_date, a.department_id,
d.hours_of_operation->to_char(a.activity_date, 'Day')->>'isOpen' as isOpen
from activity a
join department d on a.department_id = d.id
but that only returns an isOpen value when the activity_date is a Wednesday (which is the last key in the hours_of_operation jsonb)