Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取SQL(配置单元)中数组的最后N个元素_Sql_Arrays_Hive_Hiveql - Fatal编程技术网

提取SQL(配置单元)中数组的最后N个元素

提取SQL(配置单元)中数组的最后N个元素,sql,arrays,hive,hiveql,Sql,Arrays,Hive,Hiveql,我有一个包含数组的列,我想提取数组中最后的X个元素 尝试提取最后两个元素的示例: Column A ['a', 'b', 'c'] ['d', 'e'] ['f', 'g', 'h', 'i'] 预期产出: Column A ['b', 'c'] ['d', 'e'] ['h', 'i'] 最好的情况是不使用自定义项,而是使用反向、分解、过滤和重新组装数组的方法: with your_table as ( select

我有一个包含数组的列,我想提取数组中最后的X个元素

尝试提取最后两个元素的示例:

     Column A
     ['a', 'b', 'c']
     ['d', 'e']
     ['f', 'g', 'h', 'i']
预期产出:

     Column A
    ['b', 'c']
    ['d', 'e']
    ['h', 'i']

最好的情况是不使用自定义项,而是使用反向、分解、过滤和重新组装数组的方法:

with your_table as (
select stack (4,
0, array(), --empty array to check it works if no elements or less than n
1, array('a', 'b', 'c'),
2, array('d', 'e'),
3, array('f', 'g', 'h', 'i')
) as (id, col_A)
)

select s.id, collect_list(s.value) as col_A 
from
(select s.id, a.value, a.pos
  from your_table s
       lateral view outer posexplode(split(reverse(concat_ws(',',s.col_A)),',')) a as pos, value
where a.pos between 0 and 1 --last two (use n-1 instead of 1 if you want last n)  
distribute by s.id sort by a.pos desc --keep original order
)s
group by s.id
结果:

s.id    col_a
0   []
1   ["b","c"]
2   ["d","e"]
3   ["h","i"]
使用brickhouse
数值范围更优雅