生成范围和减量的序列号

时间:2017-02-01 09:26:41

标签: sql hive hiveql

我有一个如下表格,它有起始范围和结束范围列。

+------+----------+--------+--+
|  f1  | start_r  | end_r  |
+------+----------+--------+--+
| ABC  | 31       | 29     |
+------+----------+--------+--+

我需要使用start_r和end_r中的值并生成如下输出(生成开始和结束范围之间的序列号递减1)

 f1   seq_no

 ABC   31
 ABC   30
 ABC   29

我只需要一种生成值的方法..在hive中是否有任何buitin函数用于此?

2 个答案:

答案 0 :(得分:3)

编辑:
替换split(space(start_r - end_r - 1),'')
split(space(start_r - end_r),' ')

select  t.f1
       ,t.start_r - pe.i as seq_no 

from   (select 'ABC' as f1,31 as start_r,29 as end_r)                t 
       lateral view posexplode(split(space(start_r - end_r),' ')) pe as i,s
;
hive> select t.f1,t.start_r - pe.i as seq_no from (select 'ABC' as f1,31 as start_r,29 as end_r) t lateral view posexplode(split(space(start_r - end_r - 1),'')) pe as i,s;
OK
ABC 31
ABC 30
ABC 29

(select 'ABC' as f1,31 as start_r,29 as end_r)替换为您的表格

答案 1 :(得分:0)

请使用以下查询 ;与n(n)AS (     选择31
    UNION ALL     SELECT n-1 FROM n WHERE n> 29 ) SELECT n FROM n ORDER BY n Desc 选项(MAXRECURSION 1000);