LATERAL VIEW を使うと配列を展開しそれぞれの要素が結合されるのでそれを利用する。
ex)
users テーブル内の attributes にユーザの属性が登録されていて女性ユーザの人数を取得する場合。
- users table
name | type |
---|---|
user_id | string |
user_attributes | array |
- attributes array
["female", "28", "orange"]
["male", "18", "car"]
["female", "32", "art"]
SELECT count(*) FROM tracks
LATERAL VIEW explode(attributes) exploded_attributes AS attribute
WHERE
attribute = 'female'
下記の様に結合されるので条件を設定することで件数を集計できる。
user_id | attribute |
---|---|
1 | female |
1 | 28 |
1 | orange |
2 | male |
2 | 18 |
2 | car |
3 | female |
3 | 32 |
3 | art |