hive sql练习多行变多列

描述:表中记录了各年份各部门的平均绩效考核成绩
表结构:
a -- 年份
b -- 部门
c -- 绩效得分

表内容
t1.a    t1.b    t1.c
2014    B       9
2015    A       8
2014    A       10
2015    B       7

建表语句

create table t25(
  a string,
  b string,
  c int
)row format delimited
fields terminated by ',';
load data local inpath '/home/hivedata/t25.txt' into table t25;
with t1 as (
    select distinct a,concat_ws('',collect_set(concat(b,c)) over (partition by c)) temp from t25
    ),t2 as (select distinct a,
                     `if`(substr(temp,1,1)='A',substr(temp,2),null ) as col_A,
                     `if`(substr(temp,1,1)='B',substr(temp,2),null ) as col_B
                     from t1) select a,
                                     sum(case when col_A is not null then col_A end ) col_A,
                                     sum(case when col_B is not null then col_B end ) col_B
                                     from t2 group by a;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值