4

例如,我如何针对包含这些记录的表 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     |

所有列都应该有一个唯一的值。

我可以为此使用什么查询语句?

谢谢

4

3 回答 3

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

或者

select column1 as val from YourTable 
UNION
select column2 from YourTable 

VAL
-----
B
C
D
F
G
H
于 2013-11-14T13:57:33.973 回答
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');

我的期望是

高炉 CG DH

但我只得到

高炉

非常感谢!

于 2013-11-14T07:37:18.773 回答
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 小提琴演示

于 2013-11-14T06:29:55.900 回答