impala 将列转置为行

问题描述 投票:0回答:1

如何在 impala 中将列数据转置为行数据 我尝试了一些在 impala 中不起作用但在 hive 中工作的解决方案。

Table name : test
Data:
day         name     jobdone
2017-03-25  x_user   5
2017-03-25  y_user   10
2017-03-31  x_user   20
2017-03-31  y_user   1

我希望数据应该像 impala 中的数据一样,而不是 hive 中的数据

Required Output Data
 Day           x_user     y_user
 2017-03-05    5          10
 2001-03-31    20         1

我可以在 Hive 中使用 Map 和collect_list 来完成。我在 Impala 能做什么。

sql hive impala
1个回答
4
投票

使用

case
+
min()
max()
聚合:

select day,
       max(case when name='x_user' then jobdone end) x_user,
       max(case when name='y_user' then jobdone end) y_user
  from test
  group by day;

如果每个用户、每天有很多记录并且您需要对它们进行求和,请使用

sum()
而不是
max()

© www.soinside.com 2019 - 2024. All rights reserved.