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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not Sure If this is a grouping question 1

Status
Not open for further replies.

yorge

Programmer
Aug 2, 2011
39
PH
Hi Guys,

Given the sample table below:

ID X Y
-----------------------
1 25 24
2 24 25
3 75 1
4 9 10
5 10 9
-----------------------

I wanted a result like this:

ID X Y
-----------------------
2 24 25
3 75 1
5 10 9
-----------------------

When values for X and Y exists, it will always show the higher ID value.

Thanks in advance,
Yorge
 
If I understand your question correctly, then I would do it this way:

Code:
Select  Distinct 
        Case When B.ID > A.ID Then B.Id Else A.ID End As Id,
        Case When B.ID > A.ID Then B.X Else A.X End As x,
        Case When B.Id > A.ID Then B.Y Else A.Y End as y
From    YourTableNameHere As A
        Left Join YourTableNameHere As B
          On A.X = B.Y
          And A.Y = B.X

This returns the correct data based on your sample data and your expected results.


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top