简体   繁体   English

从 AWS Athena 中的数组元素分组

[英]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.我听说过unnest an array,但无法得到这个结果。

How should I approach this?我应该如何处理这个?

I think you only need to unnest :我认为你只需要unnest

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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