从 AWS Athena 中的数组元素分组
Group by from the elements of array in AWS Athena
我有一个有两列的 table。 table 具有以下架构
column_name ---> type
student_id ---> int
subjects ---> array<string>
样本数据为:
student_id ---> subjects
10 ---> [Math, Science]
20 ---> [Math, English]
30 ---> [English, French]
我想按个别科目分组,我想计算所有学生选修的科目数量。所以我的预期结果是
Math ---> 2
Science ---> 1
English ---> 2
French ---> 1
我听说过 unnest
一个数组,但无法得到这个结果。
我该如何处理?
我想你只需要 unnest
:
select subject, count(*)
from t cross join
unnest(subjects) as u(subject)
group by subject;
我有一个有两列的 table。 table 具有以下架构
column_name ---> type
student_id ---> int
subjects ---> array<string>
样本数据为:
student_id ---> subjects
10 ---> [Math, Science]
20 ---> [Math, English]
30 ---> [English, French]
我想按个别科目分组,我想计算所有学生选修的科目数量。所以我的预期结果是
Math ---> 2
Science ---> 1
English ---> 2
French ---> 1
我听说过 unnest
一个数组,但无法得到这个结果。
我该如何处理?
我想你只需要 unnest
:
select subject, count(*)
from t cross join
unnest(subjects) as u(subject)
group by subject;