Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eliminating reversed duplicate rows 1

Status
Not open for further replies.

raynalb

Programmer
Jan 7, 2011
2
0
0
US
My SQL is a little rusty. Suppose I have Table1 with fields X and Y:
X Y
-- --
A B
A C
B A

How would I write the SQL to eliminate the 3rd row because it is a duplicate of the 1st row when reversed?

Thanks,
Ray
 
Code:
select * from Table1 T1
where not exists (select * from Table1 T2 where T1.X = T2.Y and T1.Y = T2.X)

Alternative solution
Code:
select X, Y from Table1 T1
EXCEPT
select Y, X from Table1 T1

PluralSight Learning Library
 
Thanks markros. Worked great.

Regards,
Ray
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top