HIVE 数据透视和总和/计数
Posted
技术标签:
【中文标题】HIVE 数据透视和总和/计数【英文标题】:HIVE Pivot and Sum/count 【发布时间】:2021-05-20 05:19:28 【问题描述】:我有一个假数据表,我试图弄清楚如何根据这些值进行透视和求和/计数。
示例输入:
示例输出:
感谢您的帮助!
【问题讨论】:
【参考方案1】:按水果标志过滤,汇总每个结果并合并所有结果:
select 'apple_flg' as fruit_name
count(distinct student_name) as cnt_student, --if student_name is unique, no distinct needed
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
where apple_flg=1
union all
select 'banana_flg' as fruit_name
count(distinct student_name) as cnt_student,
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
where banana_flg=1
union all
select 'strawberry_flg' as fruit_name
count(distinct student_name) as cnt_student,
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
where strawberry_flg=1
union all
select 'watermelon_flg' as fruit_name
count(distinct student_name) as cnt_student,
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
where watermelon_flg=1
union all
select 'lemon_flg' as fruit_name
count(distinct student_name) as cnt_student,
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
where lemon_flg=1
另一种可能的方法:
select case when apple_flg=1 then 'apple_flg'
when banana_flg=1 then 'banana_flg'
when strawberry_flg=1 then 'strawberry_flg'
when watermelon_flg=1 then 'watermelon_flg'
when lemon_flg=1 then 'lemon_flg'
end as fruit,
count(distinct student_name) as cnt_student,
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
group by apple_flg, banana_flg, strawberry_flg, watermelon_flg, lemon_flg
如果某些水果从未购买过并且您需要那些计数为 0 的行,您可能需要更复杂的解决方案:
with fruits as (
select stack(5, 'apple_flg',
'banana_flg',
'strawberry_flg',
'watermelon_flg',
'lemon_flg'
) as fruit
),
agg as (
select case when apple_flg=1 then 'apple_flg'
when banana_flg=1 then 'banana_flg'
when strawberry_flg=1 then 'strawberry_flg'
when watermelon_flg=1 then 'watermelon_flg'
when lemon_flg=1 then 'lemon_flg'
end as fruit,
count(distinct student_name) as cnt_student,
sum(buy_cnt) as sum_buy_cnt,
sum(buy_payment) as sum_buy_payment
from tablename
group by apple_flg, banana_flg, strawberry_flg, watermelon_flg, lemon_flg
)
select f.fruit,
nvl(s.cnt_student, 0) as cnt_student,
nvl(s.sum_buy_cnt, 0) as sum_buy_cnt,
nvl(s.sum_buy_payment, 0) as sum_buy_payment
from fruits f
left join agg s on f.fruit=s.fruit
【讨论】:
感谢您的回答。成功了!!!谢谢你!以上是关于HIVE 数据透视和总和/计数的主要内容,如果未能解决你的问题,请参考以下文章