Below is the postgres table table1
:
CREATE TABLE table1 (
id INT PRIMARY KEY,
name TEXT,
skills JSON
);
with below rows inserted:
INSERT INTO table1 (id, name, skills) VALUES
(1, 'Alice', ‘[
{“sid" : 11, "description" : “Cardio"},
{"sid" : 13, "description" : “Anesthist"}
]'
),
(2, ‘Bob', ‘[
{“sid" : 10, "description" : “Gastro"},
{"sid" : 9, "description" : “Oncology"}
]’
),
(3, ‘Sam', ‘[
]’
);
Below is the desired output, upon running select
query:
id name skill
---------------------
1 Alice [“Cardio”,“Anestisht”]
2 Bob ["Gastro","Oncology"]
3 Sam []
where skill
column is TEXT
type
Tried below query
select
id, name, d ->> 'description' as skill
from table1,
json_array_elements(skills) as d
that gives duplicate rows(as shown below) with a missing row, which is incorrect
id name skill
---------------------
1 Alice Cardio
1 Alice Anestisht
2 Bob Gastro
2 Bob Oncology
2
Answers
Use
array_agg()
to aggregate the descriptions into an array:See live demo.
To return rows for people with no skills as well, here’s one way:
See live demo.
Use
jsonb_path_query_array()
. It gets you ajsonb
array as shown in your expected result, not a regular SQL array oftext
.demo at db<>fiddle
If you’re on an older version of PostgreSQL (versions 9.3-11), you can expand and collapse the array in a scalar subquery, then
coalesce()
thenull
to an empty array: