5

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]
leftjoin
  • 36,950
  • 8
  • 57
  • 116
Arun Kumar Dave
  • 121
  • 1
  • 4
  • 9

1 Answers1

6

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

select combinations(array[1,2,3,4],2);
David Phillips
  • 10,723
  • 6
  • 41
  • 54
leftjoin
  • 36,950
  • 8
  • 57
  • 116