I have a JSON field in a table like below
{
"Ui": [
{
"element": "TG1",
"mention": "in",
"time": 123
},
{
"element": "TG1",
"mention": "out",
"time": 125
},
{ "element": "TG2",
"mention": "in",
"time": 251
},
{
"element": "TG2",
"mention": "out",
"time": 259
}
]
}
My intention is to get something like below
| element | Timespent |
| TG1 | 2 |
| TG2 | 8 |
but have been completely unsuccessful.
How would I get the difference (sum of all time when mention is in - sum of all time when mention is out)?
Currently I am trying to do sum(jsonb_extract_path(data::jsonb, 'ui','hovers')->0->'when') to get the sum but cant figure how to recursively look into the json file and filter for mention.