如何编写SQL查询以从单行获取不同的值?

时间:2014-02-11 22:33:56

标签: sql database relational-algebra

我有一张表,这是多次获取单个表的交叉产品的结果。现在我可能在一行中有属于不同列的重复项。我想知道如何为A!= B编写查询!= C!= D其中A,B,C,D是列名。如何编写SQL查询以从同一行中选择唯一值?

2 个答案:

答案 0 :(得分:1)

您可以通过非常复杂的where条款或仅通过适当的join条件来解决此问题。

以下是一个例子:

select a.col as a, b.col as b, c.col as c, d.col as d
from table a join
     table b
     on b.col <> a.col join
     table c
     on c.col not in (a.col, b.col) join
     table d
     on d.col not in (a.col, b.col, c.col);

答案 1 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test ( val ) AS
SELECT LEVEL
FROM   DUAL
CONNECT BY LEVEL <= 5;

查询1

SELECT A.val AS A_val,
       B.val AS B_val,
       C.val AS C_val,
       D.val AS D_val
FROM   test A
       CROSS JOIN test b
       CROSS JOIN test c
       CROSS JOIN test d
WHERE  A.val NOT IN ( b.val, c.val, d.val )
AND    B.val NOT IN ( c.val, d.val )
AND    C.val <> D.val

<强> Results

| A_VAL | B_VAL | C_VAL | D_VAL |
|-------|-------|-------|-------|
|     1 |     2 |     3 |     4 |
|     1 |     2 |     3 |     5 |
|     1 |     2 |     4 |     3 |
|     1 |     2 |     4 |     5 |
|     1 |     2 |     5 |     3 |
|     1 |     2 |     5 |     4 |
|     1 |     3 |     2 |     4 |
|     1 |     3 |     2 |     5 |
|     1 |     3 |     4 |     2 |
|     1 |     3 |     4 |     5 |
|     1 |     3 |     5 |     2 |
|     1 |     3 |     5 |     4 |
|     1 |     4 |     2 |     3 |
|     1 |     4 |     2 |     5 |
|     1 |     4 |     3 |     2 |
|     1 |     4 |     3 |     5 |
|     1 |     4 |     5 |     2 |
|     1 |     4 |     5 |     3 |
|     1 |     5 |     2 |     3 |
|     1 |     5 |     2 |     4 |
|     1 |     5 |     3 |     2 |
|     1 |     5 |     3 |     4 |
|     1 |     5 |     4 |     2 |
|     1 |     5 |     4 |     3 |
|     2 |     1 |     3 |     4 |
|     2 |     1 |     3 |     5 |
|     2 |     1 |     4 |     3 |
|     2 |     1 |     4 |     5 |
|     2 |     1 |     5 |     3 |
|     2 |     1 |     5 |     4 |
|     2 |     3 |     1 |     4 |
|     2 |     3 |     1 |     5 |
|     2 |     3 |     4 |     1 |
|     2 |     3 |     4 |     5 |
|     2 |     3 |     5 |     1 |
|     2 |     3 |     5 |     4 |
|     2 |     4 |     1 |     3 |
|     2 |     4 |     1 |     5 |
|     2 |     4 |     3 |     1 |
|     2 |     4 |     3 |     5 |
|     2 |     4 |     5 |     1 |
|     2 |     4 |     5 |     3 |
|     2 |     5 |     1 |     3 |
|     2 |     5 |     1 |     4 |
|     2 |     5 |     3 |     1 |
|     2 |     5 |     3 |     4 |
|     2 |     5 |     4 |     1 |
|     2 |     5 |     4 |     3 |
|     3 |     1 |     2 |     4 |
|     3 |     1 |     2 |     5 |
|     3 |     1 |     4 |     2 |
|     3 |     1 |     4 |     5 |
|     3 |     1 |     5 |     2 |
|     3 |     1 |     5 |     4 |
|     3 |     2 |     1 |     4 |
|     3 |     2 |     1 |     5 |
|     3 |     2 |     4 |     1 |
|     3 |     2 |     4 |     5 |
|     3 |     2 |     5 |     1 |
|     3 |     2 |     5 |     4 |
|     3 |     4 |     1 |     2 |
|     3 |     4 |     1 |     5 |
|     3 |     4 |     2 |     1 |
|     3 |     4 |     2 |     5 |
|     3 |     4 |     5 |     1 |
|     3 |     4 |     5 |     2 |
|     3 |     5 |     1 |     2 |
|     3 |     5 |     1 |     4 |
|     3 |     5 |     2 |     1 |
|     3 |     5 |     2 |     4 |
|     3 |     5 |     4 |     1 |
|     3 |     5 |     4 |     2 |
|     4 |     1 |     2 |     3 |
|     4 |     1 |     2 |     5 |
|     4 |     1 |     3 |     2 |
|     4 |     1 |     3 |     5 |
|     4 |     1 |     5 |     2 |
|     4 |     1 |     5 |     3 |
|     4 |     2 |     1 |     3 |
|     4 |     2 |     1 |     5 |
|     4 |     2 |     3 |     1 |
|     4 |     2 |     3 |     5 |
|     4 |     2 |     5 |     1 |
|     4 |     2 |     5 |     3 |
|     4 |     3 |     1 |     2 |
|     4 |     3 |     1 |     5 |
|     4 |     3 |     2 |     1 |
|     4 |     3 |     2 |     5 |
|     4 |     3 |     5 |     1 |
|     4 |     3 |     5 |     2 |
|     4 |     5 |     1 |     2 |
|     4 |     5 |     1 |     3 |
|     4 |     5 |     2 |     1 |
|     4 |     5 |     2 |     3 |
|     4 |     5 |     3 |     1 |
|     4 |     5 |     3 |     2 |
|     5 |     1 |     2 |     3 |
|     5 |     1 |     2 |     4 |
|     5 |     1 |     3 |     2 |
|     5 |     1 |     3 |     4 |
|     5 |     1 |     4 |     2 |
|     5 |     1 |     4 |     3 |
|     5 |     2 |     1 |     3 |
|     5 |     2 |     1 |     4 |
|     5 |     2 |     3 |     1 |
|     5 |     2 |     3 |     4 |
|     5 |     2 |     4 |     1 |
|     5 |     2 |     4 |     3 |
|     5 |     3 |     1 |     2 |
|     5 |     3 |     1 |     4 |
|     5 |     3 |     2 |     1 |
|     5 |     3 |     2 |     4 |
|     5 |     3 |     4 |     1 |
|     5 |     3 |     4 |     2 |
|     5 |     4 |     1 |     2 |
|     5 |     4 |     1 |     3 |
|     5 |     4 |     2 |     1 |
|     5 |     4 |     2 |     3 |
|     5 |     4 |     3 |     1 |
|     5 |     4 |     3 |     2 |