I have a table which has two columns. The table has the following schema
column_name ---> type student_id ---> int subjects ---> array<string>
The sample data is:
student_id ---> subjects 10 ---> [Math, Science] 20 ---> [Math, English] 30 ---> [English, French]
I want to group by the individual subject that is I want to count the number of subjects that all the students have taken. So my expected result is
Math ---> 2 Science ---> 1 English ---> 2 French ---> 1
I have heard about unnest
an array, but not able to get this result.
How should I approach this?
Advertisement
Answer
I think you only need to unnest
:
select subject, count(*) from t cross join unnest(subjects) as u(subject) group by subject;