我在下面有一张表,想按从开始列到结束列的范围拆分行。
即 id 和 value 应该对开始和结束之间的每个值重复(包括两者)
--------------------------------------
id | value | start | end
--------------------------------------
1 | 5 | 1 | 4
2 | 8 | 5 | 9
--------------------------------------
所需输出
--------------------------------------
id | value | current
--------------------------------------
1 | 5 | 1
1 | 5 | 2
1 | 5 | 3
1 | 5 | 4
2 | 8 | 5
2 | 8 | 6
2 | 8 | 7
2 | 8 | 8
2 | 8 | 9
--------------------------------------
我可以在 java/python 中编写自己的 UDF 来获得此结果,但想检查是否可以使用任何现有的 hive UDF 在 Hive SQL 中实现
提前致谢。
这可以通过 Hive 不支持的递归公用表表达式来完成。
一种选择是创建一个数字表并使用它在开始和结束之间生成行。
create table numbers
location 'hdfs_location' as
select row_number() over(order by somecolumn) as num
from some_table --this can be any table with the desired number of rows
;
--Join it with the existing table
select t.id,t.value,n.num as current
from tbl t
join numbers n on n.num>=t.start and n.num<=t.end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)