Posted at

SQLで組み合わせを作ってみる

More than 1 year has passed since last update.

1から15の数字を使って要素数7の組み合わせを作る

要はこれ $$ _{15} C _7 $$

WITH element AS(

SELECT
a
FROM
UNNEST(GENERATE_ARRAY(1,15)) AS a
)

SELECT
e1.a AS a1,
e2.a AS a2,
e3.a AS a3,
e4.a AS a4,
e5.a AS a5,
e6.a AS a6,
e7.a AS a7
FROM
element AS e1,
element AS e2,
element AS e3,
element AS e4,
element AS e5,
element AS e6,
element AS e7
WHERE
e1.a < e2.a
AND e2.a < e3.a
AND e3.a < e4.a
AND e4.a < e5.a
AND e5.a < e6.a
AND e6.a < e7.a

もうちょっとスマートにしたい。

とは言えこれができたからと言って利用用途が思いついてるわけではない。