SQL:基于列复制行
我有一张这样的桌子:SQL:基于列复制行,sql,hiveql,Sql,Hiveql,我有一张这样的桌子: Name | Math | English | Arts ---------------------------------------- Brad | 87 | 65 | 90 Julie | 91 | 88 | 92 我想得到: Name | Grade -------------- Brad | 87 Brad | 65 Brad | 90 Julie | 91 Julie | 88 Julie | 9
Name | Math | English | Arts
----------------------------------------
Brad | 87 | 65 | 90
Julie | 91 | 88 | 92
我想得到:
Name | Grade
--------------
Brad | 87
Brad | 65
Brad | 90
Julie | 91
Julie | 88
Julie | 92
使用SQL/Hive最简单的方法是什么
select name,math as Grade from your_table
union all
select name,English as Grade from your_table
union all
select name,Arts as Grade from your_table
像这样的
select name,math as Grade from your_table
union all
select name,English as Grade from your_table
union all
select name,Arts as Grade from your_table
您可以使用
unpivot
:
SELECT X.Name, X.Grade
FROM your_table s
UNPIVOT
(
Grade
FOR Subject in (Maths, English, Arts)
) X;
如果要在结果中添加主题,请将
X.subject
添加到select语句中。您可以使用unpivot
:
SELECT X.Name, X.Grade
FROM your_table s
UNPIVOT
(
Grade
FOR Subject in (Maths, English, Arts)
) X;
如果要让结果中的主题将
X.subject
添加到select语句中。我可以让ot在配置单元上运行,您确定它支持UNPIVOT
?我还假设它是在你的表之后
对不起,我不太清楚蜂巢。我的回答是关于使用SQL解决它。在配置单元中有一个unpivot示例可能会有所帮助。我可以让ot在配置单元上运行,您确定它支持unpivot
?我还假设它是在你的表之后
对不起,我不太清楚蜂巢。我的回答是关于使用SQL解决它。在蜂巢中有一个unpivot示例,可能会有所帮助。