Presto SQL-如何获得数组的所有可能组合?

时间:2019-06-11 09:20:18

标签: arrays database hive presto

我想要给定数组中所有可能的数字组合。

我尝试使用presto的一些预定义功能,例如array_agg(x)

Input : [1,2,3,4]
Output
when n=2 : [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
when n=3 : [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
when n=4 : [[1,2,3,4]] or [1,2,3,4]

1 个答案:

答案 0 :(得分:6)

combinations(array(T), n) 函数,它可以完全满足您的要求:

select combinations(array[1,2,3,4],2);