我在sql server数据库中有一个表。
Source Destination Fare
-------------------------------
Delhi Mumbai 100
Mumbai Delhi 100
London New York 500
我必须编写一个生成以下结果的 SQL 查询。
Source Destination Fare
-------------------------------
Delhi Mumbai 100
London New York 500
如果交换源和目标并且它与任何前一行匹配,则将其删除。
最佳答案
一种方法使用union all
并且不存在
:
select source, destination, fare
from t
where source < destination
union all
select source, destination, fare
from t
where source > destination and
not exists (select 1
from t t2
where t2.source = t.destination and t2.destination = t.source
);
关于sql - 删除重复的源到目的地(例如,对于 2 个不同的条目(行),德里到孟买和孟买到德里,输出应该是一个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54533518/