I am (still) new to postgresql and jsonb. I am trying to select some records from a subquery and am stuck. My data column looks like this (jsonb):
{"people": [{"age": "50", "name": "Bob"}], "another_key": "no"}
{"people": [{"age": "73", "name": "Bob"}], "another_key": "yes"}
And here is my query. I want to select all names that are "Bob" whose age is greater than 30:
SELECT * FROM mytable
WHERE (SELECT (a->>'age')::float
FROM (SELECT jsonb_array_elements(data->'people') as a
FROM mytable) as b
WHERE a @> json_object(ARRAY['name', 'Bob'])::jsonb
) > 30;
I get the error:
more than one row returned by a subquery used as an expression
I don't quite understand. If I do some simple substitution (just for testing) I can do this:
SELECT * FROM mytable
WHERE (50) > 30 -- 50 is the age of the youngest Bob
and that returns both rows.