Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在Oracle表中匹配对角线记录_Sql_Oracle_Oracle11g - Fatal编程技术网

Sql 如何在Oracle表中匹配对角线记录

Sql 如何在Oracle表中匹配对角线记录,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我有一个名为test的表,它有3列src、dest和distance,格式如下 src dest distance ----------------------------------------- Newyork Texas 680 Texas Newyork 680 Newyork Florida 210 Florida Newyork

我有一个名为test的表,它有3列src、dest和distance,格式如下

src             dest          distance
-----------------------------------------
Newyork         Texas         680
Texas           Newyork       680
Newyork         Florida       210
Florida         Newyork       210
Florida         California    490
如果src和dest之间的距离相同,即纽约和得克萨斯之间的距离与得克萨斯和纽约之间的距离相同,则我希望在最终输出中将其显示为1个条目,而不是2个条目,如下所示

src             dest          distance
-----------------------------------------
Newyork         Texas         680
Newyork         Florida       210
Florida         California    490
有人能帮我们查询一下吗。

您可以使用最小值/最大值:

您可以使用最小值/最大值:

这里有一种方法:

select t.src, t.dest, t.distance
from t
where t.src < t.dest
union all
select t.src, t.dest, t.distance
where t.src > t.dest and
      not exists (select 1
                  from t t2
                  where t2.src = t.dest and t2.dest = t.src and
                        t2.distance = t.distance
                 );
这里有一种方法:

select t.src, t.dest, t.distance
from t
where t.src < t.dest
union all
select t.src, t.dest, t.distance
where t.src > t.dest and
      not exists (select 1
                  from t t2
                  where t2.src = t.dest and t2.dest = t.src and
                        t2.distance = t.distance
                 );

如果列的顺序很重要,则可以使用单个“不存在”:

SELECT -- DISTINCT -- might be needed depending on possible duplicate
   src,dest,distance
FROM tab t1
WHERE NOT EXISTS(
  SELECT * FROM tab t2
  WHERE t1.src = t2.dest
    AND t1.dest = t2.src
    AND t1.src > t2.src
    AND t1.distance = t2.distance
);

如果列的顺序很重要,则可以使用单个“不存在”:

SELECT -- DISTINCT -- might be needed depending on possible duplicate
   src,dest,distance
FROM tab t1
WHERE NOT EXISTS(
  SELECT * FROM tab t2
  WHERE t1.src = t2.dest
    AND t1.dest = t2.src
    AND t1.src > t2.src
    AND t1.distance = t2.distance
);

这不会在结果集中保留src和dest的原始值。@GordonLinoff您说得对,如果只有一对,则可以交换值。如果顺序是关键的,那么您的解决方案将处理它。这不会在结果集中保持src和dest的原始值。@GordonLinoff您是对的,如果只有一对,那么它可以交换值。如果订单至关重要,那么您的解决方案将处理它。