I’m getting an array of values as part of a query:
SELECT class_id, count(*) as num_in_class, ARRAY_AGG(DISTINCT major_id) major_ids
FROM students GROUP BY class_id;
This gives me the number of distinct majors in each class. The id of the major isn’t super helpful though, I would like to know the name of the major:
SELECT major.name, ...
...
JOIN major on major.id = major_id
This naive solution doesn’t work as is because my major_id
s are in an array. How can I complete this join and get the names of the majors into an array major_names
of the same length as major_ids
?
2
Answers
Do you just want something like this query?
Its basically the same that you are proposing with the join
Fiddle to test