选择不同于表 - 两列没有列应该重复

时间:2013-11-14 06:19:02

标签: sql oracle

如何对表A进行选择,例如包含这些记录。

|Column1|Column2|
| A      |F     |
| A      | G    |
| B      |G     |
| B      |H     |
| C      |H     |
| D      |H     |
| E      |I     |

我的预期结果是:

|Column1 |Column2|
| A      | F     |
| B      | G     |
| C      | H     |
| E      | I     |

所有列都应包含唯一值。

我可以使用哪种查询语句?

由于

3 个答案:

答案 0 :(得分:1)

请尝试:

select 
    MIN(Column1) Column1, 
    Column2 
from(
    select 
        Column1, 
        MIN(Column2) Column2 
    from YourTable
    group by Column1 
)x group by Column2
order by 1

SQL Fiddle Demo

答案 1 :(得分:1)

它不适合这种情况。

create table YourTable (Column1 varchar2(10), 
                        Column2 varchar2(10));

insert into YourTable values ('B','F');
insert into YourTable values ('B','G');
insert into YourTable values ('B','H');
insert into YourTable values ('C','F');
insert into YourTable values ('C','G');
insert into YourTable values ('C','H');
insert into YourTable values ('D','F');
insert into YourTable values ('D','G');
insert into YourTable values ('D','H');

我的期望是

B F. C G. D H

但我只有

B F

非常感谢!

答案 2 :(得分:1)

SELECT a.val, b.val FROM
(
 SELECT val, rownum as rno 
  FROM
  (
   SELECT distinct column1 as val
     FROM YourTable 
  )) a,
  (
   SELECT val, rownum as rno 
     FROM
  (
    SELECT distinct column2 as val
      FROM YourTable 
  )) b
  WHERE a.rno = b.rno
  ORDER BY 1
  /

VAL VAL_1
-----------
 B  F
 C  G
 D  H

OR

select column1 as val from YourTable 
UNION
select column2 from YourTable 

VAL
-----
B
C
D
F
G
H