gpt4 book ai didi

hadoop - HIVE 数据透视和求和

转载 作者:可可西里 更新时间:2023-11-01 14:46:28 31 4
gpt4 key购买 nike

我有一个表,我试图找出如何根据第二列中的值进行数据透视和求和。

示例输入:

|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 |

示例输出:

|own|dog|cat|cow|
|---|---|---|---|
|bob| 6 | 1 | |
|jon| 1 | 2 | 4 |
|sam| 1 | | 3 |

最佳答案

使用casesum():

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;

关于hadoop - HIVE 数据透视和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770037/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com