SQL / HIVE-如何从水平输出查询到垂直输出?

时间:2018-08-02 14:34:07

标签: hive hiveql

我有一个查询,该查询返回两行包含所需信息的信息

SELECT src_file_dt, a, b ,c FROM my_table WHERE src_file_dt IN ('1531675040', '1531675169');

它将返回:

src_file_dt | a | b | c  
1531675040  | 2 | 6 | 9  
1531675169  | 8 | 2 | 0 

现在,我需要以下布局中的数据,如何获得如下所示的输出:

fields | prev (1531675040) | curr (1531675169)
a      |   2               | 8
b      |   6               | 2
c      |   9               | 0

1 个答案:

答案 0 :(得分:0)

应该有更简单的方法来实现这一目标。 我使用explode函数并为上一列和下一列分别选择数据来构建结果:

select t1.keys, t1.vals as prev, t2.vals as next from (
SELECT explode(map('a', a, 'b', b, 'c',c)) as (keys, vals)
FROM my_table 
WHERE src_file_dt = '1531675040'
) t1, 
(
SELECT explode(map('a', a, 'b', b, 'c',c)) as (keys, vals)
FROM my_table 
WHERE src_file_dt = '1531675169'
) t2
where t1.keys = t2.keys
;