Jay*_*Jay 4 hadoop transpose hive pivot sum
我有一个表格,我想弄清楚如何根据第二列中的值进行透视和求和。
示例输入:
|own|pet|qty|
|---|---|---|
|bob|dog| 2 |
|bob|dog| 3 |
|bob|dog| 1 |
|bob|cat| 1 |
|jon|dog| 1 |
|jon|cat| 1 |
|jon|cat| 1 |
|jon|cow| 4 |
|sam|dog| 3 |
|sam|cow| 1 |
|sam|cow| 2 |
Run Code Online (Sandbox Code Playgroud)
示例输出:
|own|dog|cat|cow|
|---|---|---|---|
|bob| 6 | 1 | |
|jon| 1 | 2 | 4 |
|sam| 1 | | 3 |
Run Code Online (Sandbox Code Playgroud)
使用case和sum():
select own, sum(case when pet='dog' then qty end) as dog,
sum(case when pet='cat' then qty end) as cat,
sum(case when pet='cow' then qty end) as cow
from your_table
group by own;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8753 次 |
| 最近记录: |