Aggregation On Struct columns Hive Aggregation On Struct columns Hive hadoop hadoop

Aggregation On Struct columns Hive


select      t.name            ,count   (e.col.n_nationkey)             as count            ,sum     (e.col.n_nationkey)             as sum           ,count   (distinct e.col.n_nationkey)    as distinct_val from        temp t lateral view explode (t.nations) egroup by    t.name order by    t.name;

For the OP

The same solution with an alias.
nations is not a struct. It is an array of structs.
It does not have an n_nationkey attribute. It has struct elements that have n_nationkey attributes.
The explode function takes an array of structs (nations) and return each struct (nation) in a separate row.

select      t.name            ,count   (e.nation.n_nationkey)             as count            ,sum     (e.nation.n_nationkey)             as sum           ,count   (distinct e.nation.n_nationkey)    as distinct_val from        temp t lateral view explode (t.nations) e as nationgroup by    t.name order by    t.name;