简体   繁体   中英

Group by from the elements of array in AWS Athena

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?

I think you only need to unnest :

select subject, count(*)
from t cross join
     unnest(subjects) as u(subject)
group by subject;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM