SQL and unique n-column combinations

Deadly 提交于 2019-12-22 05:03:40

问题


Is there a simple way on Oracle to query unique combinations of n fields. I Have very simple two-field solution:

CREATE TABLE combinations AS
SELECT 1 AS n 
  FROM DUAL
UNION ALL
SELECT 2
  FROM DUAL;

Querying for unique combinations:

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 <> c2.n) x
GROUP BY LEAST(x.a, x.b), GREATEST(x.a,x.b);

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?


回答1:


Your query for 2 columns could be rewritten like this:

SELECT
  c1.n,
  c2.n
FROM combinations c1
  INNER JOIN combinations c2 ON c1.n < c2.n

For 3 columns you would then need to make some additions (highlighted in bold):

SELECT
  c1.n,
  c2.n,
  c3.n
FROM combinations c1
  INNER JOIN combinations c2 ON c1.n < c2.n
  INNER JOIN combinations c3 ON c2.n < c3.n

I'm sure you can now easily guess how to scale this for more columns.




回答2:


Your solution lies in using a pipelined table function to return your combinations.

Your function can live in its own package (with package state) that returns the data you need, in the order you wish. The state would be a set of variables determined by the number of columns to return, initialized to the initial starting values (A=1, B=A+1, C=B+1, D=C+1, etc.). Then you merely

PIPE ROW(a,b,c,d);
-- increment D
-- if D overflow limit, increment C and re-init D to C+1
-- if C overflow limit-1, increment B and re-init C to B+1
-- if B overflow limit-2, increment A and re-init B to A+1
-- if A overflow limit-3, the end

This outputs, for N = 6

 1,2,3,4
 1,2,3,5
 1,2,3,6
 1,2,4,5
 1,2,4,6
 1,3,4,5
 1,3,4,6
 2,3,4,5
 2,3,4,6
 2,3,5,6
 3,4,5,6


来源:https://stackoverflow.com/questions/5924118/sql-and-unique-n-column-combinations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!