选择1条记录,其中两条记录在不同的列中具有相同的值

时间:2013-06-03 14:23:35

标签: sql oracle

我有一个包含两列的表。

Table snap

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

2 个答案:

答案 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"

SQL Fiddle

答案 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
相关问题