PL/SQL Tips

How To Query 2 columns of data that contain duplicate entries but return only distinct matches:

create table TEST_DUP(COL_A number, COL_B number);

insert into TEST_DUP values(1,4);
insert into TEST_DUP values(2,5);
insert into TEST_DUP values(3,6);
insert into TEST_DUP values(4,1);
insert into TEST_DUP values(5,2);
insert into TEST_DUP values(6,3);

select *
from TEST_DUP;

COL_A    COL_B
1                4
2               5
3               6
4               1
5              2
6              3
select distinct least(COL_A,COL_B) COL_A
, greatest(COL_A,COL_B) COL_B
from TEST_DUP

COL_A    COL_B
1                4
2               5
3               6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s