SQL: How to Return One DB Row from Two That Have The Same Values In Opposite Columns Using the MAX F

Posted by OneSource on Stack Overflow See other posts from Stack Overflow or by OneSource
Published on 2010-05-31T21:56:28Z Indexed on 2010/05/31 22:03 UTC
Read the original article Hit count: 232

Filed under:
|
|

Hi,

This is what I'm trying to do. I have three columns in a table - ID, Column1, Column2 - with this example data:

ID Column1 Column2
1     1            2
2     2            1
3     4            3
4     3            4

Since, in the first two rows, Column1 and Column2 have the same values (but in different columns), I want my MAX query to return an ID of 2. Same thing with rows 3 and 4 .... since Columns 1 and 2 have the same values (but in different columns), I want MAX(ID) to return 4. Of course, with MAX, you use Group By, but that will not work in my case.

In effect, I need a Group By to work across two columns. Is this possible? If not, what's the best way to accomplish getting the IDs of 2 and 4 given the matching values that are in different columns?

Thanks!

© Stack Overflow or respective owner

Related posts about tsql

Related posts about group-by