I have this JSON field on each row of the table that records who was involved in a safety incident in a risk management tool, and what their role was in that incident.
'roles':[
{
"user":
{
"user_id": "c4456c9c-880b-4d1f-8066-47d77e2b6eac",
"name": "James Dean",
},
"role": "170"
},
{
"user":
{
"user_id": "cd91f708-12cd-4541-9022-6gf6a44a2e43",
"name": "John Smith",
},
"role": "172"
}
]
I need a summary of the role by user. The number of roles is unknown, although is usually between 2-6 roles. The end result of this report will look something like:
e.g.
| User | Role 170 | Role 171 | Role 172 |
|---|---|---|---|
| James Dean | 1 | 0 | 0 |
| John Smith | 0 | 0 | 1 |
Currently we pull the entire object and process it in PHP but I'd like to push as much work back on to postgres as possible so the agregations are faster.
We are using Laravel 8 and Postgres 12.8 - the column is json not jsonb (I'm not sure if that matters) - thanks!