Click here to Skip to main content
15,923,273 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
x y
20 20
20 20
21 22
22 21
23 24
24 25
25 24

i have a above table with 2 columns.i need to filter out similar set of data from 2 columns and display as one set.in brief my output should be as below :

x y
20 20
21 22
24 25

basically i want records with similar set e.g 21,22 and 22,21 are considered as similar set and hence i want to display only one record as 21,22.Whereas 23,24 doesn't have similar set(24,23) in the table so i want to eliminate that record.

What I have tried:

i tried group by but it isn't working
Posted
Updated 18-Jan-18 4:10am
v2
Comments
OriginalGriff 18-Jan-18 2:18am    
Sorry, but that doesn't make a lot of sense: you need to explain in better detail why some of the results from that input are included and others not: What happened to "23" for example? "22, 21" isn;t there either. Why not?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 13629523 18-Jan-18 2:35am    
Have updated my question.

This query removes duplicates, if you want multiple identical pairs it won't work.
SQL
WITH r as (
    SELECT  X
           ,Y
           ,ROW_NUMBER() OVER (ORDER BY X,Y) rn
    FROM    t
    )
SELECT  DISTINCT x,y
FROM    r t1
WHERE EXISTS (
    SELECT  x, y
    FROM    r t2
    WHERE   t1.x=t2.y
        AND t1.y=t2.x
        AND t1.rn<t2.rn)

SQL Fiddle[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 18-Jan-18 10:16am    
5
select table1.id, table1.x, table1.y
from yourtable table1
where exists (select x, y
from yourtable table2
where table1.x=table2.y and table1.y=table2.x)
Union
select t1.x, t1.y
from yourtable t1
where exists (select x, y
from yourtable t2
where t1.x=t2.y and t1.x=t2.y)
and t1.x<t1.y


Please don't copy paste the code directly.I believe there is some syntactical issue.First resolve that and then the main logic will be with"and" "or" operators.
I have tested this solution for the second query after union in sql fiddle.Please vote it will indeed help the community.

SQL Fiddle[^]
 
Share this answer
 
v3
Comments
Member 13629523 18-Jan-18 2:31am    
i don't have id column in my table.
[no name] 18-Jan-18 2:59am    
Can you remove group by and having and then give a try
Member 13629523 18-Jan-18 3:14am    
not working removing having and group by also.
[no name] 18-Jan-18 3:18am    
Remove condition after "OR",you'll get data like "20 20"then union

select table1.id, table1.x, table1.y
from yourtable table1
where exists (select x, y
from yourtable table2
where table1.x=table2.y and table1.y=table2.x)

and try running two separate queries before writting Union
Member 13629523 18-Jan-18 3:31am    
its just removing 20,20 duplicates and giving only one entry for 20,20.Not working for other sets.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900