PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Combinazioni PostgreSQL distinte dalle permutazioni

WITH  RECURSIVE 

     A(i) AS (SELECT * FROM unnest(ARRAY['A,B'])),
     B(j) AS (SELECT * FROM unnest(ARRAY['A','B','C','D'])),

     cte AS (  SELECT j AS combo, j, 1 AS ct 
               FROM B 
             UNION ALL 
               SELECT cte.combo ||','||B.j, B.j, ct + 1 
               FROM cte, B
               WHERE ct <= 4
                AND position(B.j in cte.combo) = 0
            )
    , com AS ( SELECT A.i ||','|| B.j AS cmb
               FROM A
               CROSS JOIN B)

     SELECT ARRAY(SELECT cmb FROM cte,com where cte.combo = com.cmb)