hive-为不同的列值插入行

xxls0lw8  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(407)

老实说,我不知道如何简单地描述标题行的问题,而不是显示一个例子。
我有一个包含两列的配置单元表:id和date

ID      Date
 31    01-01-2017
 31    01-02-2017
 31    01-03-2017
123    01-01-2017
123    01-01-2017
...

在这个表中,我想包括另一列,这是小时,如下面

ID      Date        Hour
 31    01-01-2017      00
 31    01-01-2017      01
 31    01-01-2017      02
 31    01-01-2017      03
 31    01-01-2017      04
...
 31    01-01-2017      23
 31    01-02-2017      00
 31    01-02-2017      01
...

基本上,对于每一行,我想添加一个从00到23的小时值列。这能用Hive实现吗?非常感谢。

inkz8wg9

inkz8wg91#

您可以创建一个临时表,其中包含从0到23的条目,并与现有的表进行交叉联接。或者您可以利用cte函数中的一个cte表,其中的条目从0到23,然后对其进行交叉联接。
举个例子:

with temp as (
select 0 hour union all
select 1 hour union all
select 2 hour union all
select 3 hour union all
select 4 hour union all
select 5 hour union all
select 6 hour union all
select 7 hour union all
select 8 hour union all
select 9 hour union all
select 10 hour union all
select 11 hour union all
select 12 hour union all
select 13 hour union all
select 14 hour union all
select 15 hour union all
select 16 hour union all
select 17 hour union all
select 18 hour union all
select 19 hour union all
select 20 hour union all
select 21 hour union all
select 22 hour union all
select 23 hour
)
select * from table join temp

还可以将结果插入表中以持久化结果。希望有帮助

相关问题