我有一个包含两列的表。
在前两行中,列的值相反,如何为每个实例选择一个记录,其中一个STATION_1_I等于另一个记录STATION_2_I且其STATION_2_I等于STATION_1_I。
答案 0 :(得分: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"
答案 1 :(得分: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