我有一张表XXXTEST
table XXXTEST (C1 varchar2(50),C2 varchar2(50), dist NUMBER )
使用此示例数据:
('Pune','Mumbai',128);
('Mumbai','Pune',128);
('Pune','Nashik',200);
('Nashik','Pune',200);
('Nashik','Mumbai',250);
('Nashik','Mumbai',250);
我想只选择一次城市组合,即“pune-mumbai”和“mumbai-pune”只能选择一行。
我尝试使用自联接,但这没有帮助。所以,如果您可以提供查询请。
提前致谢。
答案 0 :(得分:2)
您可以按字母顺序对城市进行排序,然后使用普通DISTINCT
:
select distinct
least(c1, c2) as c1,
greatest(c1,c2) as c2,
dist
from XXXTEST
答案 1 :(得分:1)
如果c2> c1,检查切换的组合是否存在,使用NOT EXISTS,切换的c1 / c2列:
select distinct c1, c2, dist
from XXXTEST t1
where c1 < c2
or not exists (select * from XXXTEST t2
where t1.c1 = t2.c2 and t1.c2 = t2.c1);
执行为:
SQL>CREATE TABLE XXXTEST (C1 VARCHAR(10),C2 VARCHAR(10), dist integer);
SQL>INSERT INTO xxxtest VALUES ('Pune','Mumbai',128);
SQL>
SQL>INSERT INTO xxxtest VALUES ('Mumbai','Pune',128);
SQL>INSERT INTO xxxtest VALUES ('Pune','Nashik',200);
SQL>INSERT INTO xxxtest VALUES ('Nashik','Pune',200);
SQL>INSERT INTO xxxtest VALUES ('Nashik','Mumbai',250);
SQL>INSERT INTO xxxtest VALUES ('Nashik','Mumbai',250);
SQL> select distinct c1, c2, dist
SQL& from XXXTEST t1
SQL& where c1 < c2
SQL& or not exists (select * from XXXTEST t2
SQL& where t1.c1 = t2.c2 and t1.c2 = t2.c1);
C1 C2 dist
========== ========== ===========
Mumbai Pune 128
Nashik Mumbai 250
Nashik Pune 200
3 rows found
答案 2 :(得分:0)
HIERARCHICAL方法:
SQL> SELECT DISTINCT c1,
2 c2,
3 dist
4 FROM
5 ( SELECT c1, c2, dist FROM xxxtest CONNECT BY nocycle prior c1 = c2
6 )
7 WHERE c1 > c2
8 /
C1 C2 DIST
---------- ---------- ----------
Pune Mumbai 128
Nashik Mumbai 250
Pune Nashik 200
SQL>
另外,让我们说如果城市之间的距离是唯一的,我想你可以简单地选择DISTINCT距离,而不是让它与城市及其组合变得复杂。
分析方法:
SQL> WITH DATA AS
2 (SELECT c1,
3 c2,
4 dist,
5 row_number() over(partition BY dist order by dist) rn
6 FROM xxxtest
7 )
8 SELECT c1, c2, dist FROM DATA WHERE rn = 1
9 /
C1 C2 DIST
---------- ---------- ----------
Pune Mumbai 128
Pune Nashik 200
Nashik Mumbai 250
SQL>