I have a table in postgresql, which contains JSON list, each element of is also a list (it could be dictionary though I decided to make it list or tuple).
Here is an example:
+---------------------------------------------------+
| [["status", 10], ["status", 20]] |
+---------------------------------------------------+
| [["extra", 21], ["status", 15]] |
+---------------------------------------------------+
| [["value", 33]] |
+---------------------------------------------------+
| [["extra", 21], ["status", 15], ["feature", 11]] |
+---------------------------------------------------+
I would like to find maximum status, i.g. zero-th element should be equal to "status" and get maximum value of the first element. So far I could not event extract the data, here is what I've tried:
SELECT value->0 FROM jsonb_array_elements((SELECT items::jsonb FROM mytable LIMIT 1));
With this query I can expand first row (LIMIT 1), remove limitation gives me an error:
ERROR: more than one row returned by a subquery used as an expression
I believe you can emulate my situation with next query:
select * from json_array_elements((select * from json_array_elements(
'[ [["status", 10], ["status", 20]], [["extra", 21], ["status", 15]], [["value", 33]], [["extra", 21], ["status", 15], ["feature", 11]] ]'
)));
Could someone help me to expand/flatten values?