1

我有一个包含两列的表。

表格捕捉

在前两行中,列的值是相反的,我如何为每个实例选择一个记录,其中一个 STATION_1_I 等于另一个记录 STATION_2_I 并且它的 STATION_2_I 等于 STATION_1_I。

4

2 回答 2

3

INTERSECT将为您删除重复项

select "station_1_I", "station_2_I" from mytable
intersect
select "station_2_I", "station_1_I" from mytable 
             where "station_2_I" < "station_1_I"

SQL小提琴

于 2013-06-03T14:27:55.337 回答
2
select a,b from
(
select 
(case when a<b then a else b end) as a,
(case when a>b then a else b end) as b
from (
select station_1_I as a, station_2_I as b from MyTable
union all
select station_2_I, station_1_I from MyTable
) having count(*)=2 group by a,b
) group by a,b
于 2013-06-03T14:26:54.483 回答