Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query rows and include rows with columns reversed

I’m trying to query a table. I want the results to include the FROM and TO columns, but then also include rows with these two values reversed. And then I want to eliminate all duplicates. (A duplicate is the same two cities in the same order.)

For example, given this data.

Trips

FROM                 TO
-------------------- --------------------
West Jordan          Taylorsville
Salt Lake City       Ogden
West Jordan          Taylorsville
Sandy                South Jordan
Taylorsville         West Jordan

I would want the following results.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

West Jordan          Taylorsville
Taylorsville         West Jordan
Salt Lake City       Ogden
Ogden                Salt Lake City
Sandy                South Jordan
South Jordan         Sandy

I want to do this using C# and Entity Framework, but I could use raw SQL if I need to.

Is it possible to do this in a query, or do I need to manually perform some of this logic?

>Solution :

Not sure if I’m following, but doesn’t just a simple union work for your sample?

select from, to
  from some_table
 union
select to, from
  from some_table
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading