Presto SQL-如何获得数组的所有可能组合? [英] Presto SQL - How can i get all possible combination of an array?

查看:139
本文介绍了Presto SQL-如何获得数组的所有可能组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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]

推荐答案

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

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

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

这篇关于Presto SQL-如何获得数组的所有可能组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆