title: SQL and unique n-column combinations
body: <p>Is there a simple way on Oracle to query unique combinations of n fields. I Have very simple two-field solution:</p>
  
  <pre><code>CREATE TABLE combinations AS
  SELECT 1 AS n 
    FROM DUAL
  UNION ALL
  SELECT 2
    FROM DUAL;
  </code></pre>
  
  <p>Querying for unique combinations:</p>
  
  <pre><code>SELECT LEAST(x.a, x.b), 
         GREATEST(x.a,x.b) 
    FROM (SELECT c1.n a, 
                 c2.n b 
            FROM combinations c1
      CROSS JOIN combinations c2
           WHERE c1.n &lt;&gt; c2.n) x
  GROUP BY LEAST(x.a, x.b), GREATEST(x.a,x.b);
  </code></pre>
  
  <p>From this query 1,2 and 2,1 are considered the same. Unfortunately it doesn't work for 3-field structure (for example 1,2,3 must be considered the same as 3,1,2 because ordering of values doesn't matter). Does Oracle analytic functions provide appropriate solution for this question? Could you suggest some particular Oracle analytic function?</p>