Presto SQL - How can i get all possible combination of an array?

空扰寡人 提交于 2021-01-22 09:31:43

问题


I want all the possible combination of a number in a given set of array.

I tried using some of the predefined functions of presto like 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:


There is combinations(array(T), n) function and it does exactly what you want:

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


来源:https://stackoverflow.com/questions/56540393/presto-sql-how-can-i-get-all-possible-combination-of-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!