2014-03-05 6 views
0

SQL 쿼리 대체 행에서 어떻게 제거 할 수 있습니까?SQL 쿼리에서 대체 행을 삭제하십시오. Oracle

1 : 2 
2 : 1 

어떻게 두 번째 행을 제거 할 수 있습니다 : 예를 들어 쿼리에 대한

는 다음과 같이 반환? 하나 개의 문장으로 할

+2

2, 4, 6, ... 행을 필터링 하시겠습니까? 또는 (a, b) = (b, a) 인 행을 필터링하고 싶습니까? 그리고 "필터"를 사용하면 쿼리 결과에서 해당 필터를 제거하겠습니까, 아니면 모두 삭제할 것입니까? –

+0

그건 좋은 질문입니다! 많은 가정으로 이어진다. –

+0

나는 (a, b) = (b, a) – dotnetPiotr

답변

0

아주 쉽게 : 여기

create table myTable as 
select 1 col1, 2 col2 from dual 
union all 
select 2 col1, 1 col2 from dual; 

select * from myTable; 

delete from myTable outtab 
where exists (select null 
       from myTable 
       where col1 = outtab.col2 
       and col2 = outtab.col1 
       and col2 > col1); 

select * from myTable; 

drop table myTable; 

중요한 점입니다 : 행의 두하지만 하나를 제거하는 것을 허용하지 않습니다

col2 > col1 

.

는 완전히 동일한 데이터 집합에서 제거하려면 다음

select * 
from myTable outtab 
where exists (select null 
       from myTable 
       where col1 = outtab.col2 
       and col2 = outtab.col1 
       and col2 > col1); 

또는 not exists, 당신이 원하는대로.

관련 문제