HIVE :行列转换

举个栗子

a b 1
a b 2
a b 3
c d 4
c d 5
c d 6
<==>
a b 1,2,3
c d 4,5,6

行转列 ==>

select 
col1,
col2,
concat_ws(',',collect_set(col3)) 
from table_test  
group by col1,col2;

列转行 <==

select col1, col2, col5
from table_test a
lateral  view explode(split(col3,','))  b AS col5

-- 列转行并打标
select
split(b.value,':')[0] as dim_code
,split(b.value,':')[1] as value
from
(
    select concat(
        'tx_amt:',tx_amt
        ,',refd_amt:', refd_amt
        ,',create_ordr_cnt:',create_ordr_cnt
    ) as value
    from sdm.sdm_f02_pay_sfjh_tx_sum_i_d
    where dt='2021-07-16' and dim_code='00'
)t
lateral view explode(split(value,',')) b AS value

复制  dim_code    value
1   tx_amt  395018772.92
2   refd_amt    2456561.43
3   create_ordr_cnt 10619312
附: HQL中的concat函数,concat_ws函数,concat_group函数之间的区别

你可能感兴趣的:(HIVE :行列转换)