转置来自现有列的 Hive SQL 数据
Transpose Hive SQL data from existing column
我目前正在使用以下查询来获得以下结果
select
mt.name,
mt.title,
mt.type,
mt.day1,
mt.day2
from mytable mt
给我这样的结果
name title type day1 day2
batman super gothom 12/22/1990 2/2/1990
batman super dc 1/9/1990 7/5/1990
新选择应根据类型创建新列,并从特定列中获取数据'in this case day2'我希望我的结果看起来像
name title gothom dc
batman super 2/2/1990 7/5/1990
我怎样才能达到上面想要的table。
在 Hive 中,您可以使用条件聚合来透视数据:
select mt.name, mt.title,
max(case when type = 'gothom' then day2 end) as gothom,
max(case when type = 'dc' then day2 end) as dc
from mytable mt
group by mt.name, mt.title;
我目前正在使用以下查询来获得以下结果
select
mt.name,
mt.title,
mt.type,
mt.day1,
mt.day2
from mytable mt
给我这样的结果
name title type day1 day2
batman super gothom 12/22/1990 2/2/1990
batman super dc 1/9/1990 7/5/1990
新选择应根据类型创建新列,并从特定列中获取数据'in this case day2'我希望我的结果看起来像
name title gothom dc
batman super 2/2/1990 7/5/1990
我怎样才能达到上面想要的table。
在 Hive 中,您可以使用条件聚合来透视数据:
select mt.name, mt.title,
max(case when type = 'gothom' then day2 end) as gothom,
max(case when type = 'dc' then day2 end) as dc
from mytable mt
group by mt.name, mt.title;