I have a Postgres DB with tables for templates and responses, each template defines how many rows and items it has, the response places the items in rows. I want to be able to query a merged response with the "average" placement across all responses for any template.
template_post
| id | data |
|---|---|
| 1 | {"items":[{"id":0},{"id":1},{"id":2},{"id":3}],"rows":[{"id":"0","title":"First row"},{"id":"1","title":"Second row"},{"id":"3","title":"Third row"},{"id":"4","title":"Fourth row"}]} |
| 2 | ... |
response_post
| id | template_id [FK] | data |
|---|---|---|
| 1 | 1 | {"filled_rows":[{"row_id":4,"item_ids":[0,1,3]}]} |
| 2 | 1 | {"filled_rows":[{"row_id":2,"item_ids":[2]}]} |
| 3 | 1 | {"filled_rows":[{"row_id":0,"item_ids":[1]},{"row_id":2,"item_ids":[2]},{"row_id":4,"item_ids":[3]}]} |
In this case, the query should return the following merged response:
{
"filled_rows": [
{
"row_id": 0,
"item_ids": [1]
},
{
"row_id": 2,
"item_ids": [2]
},
{
"row_id": 4,
"item_ids": [0,3]
}
]
}
In cases of ambiguity, when an item appears the same number of times in more than one row, it should be placed in the first one of those rows.
You need a few levels of aggregation here.
filled_rowsanditem_idsinto separate rows.item_idand take the minrow_id.row_idand take a JSON array ofitem_ids.db<>fiddle