Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 sql show matches based on two fields

Author  Topic 

kofseattle
Starting Member

8 Posts

Posted - 2014-03-24 : 11:39:15


0

down vote

favorite



I hope I can explain this properly, it is a little confusing. I need to query for records that have the same "origin" and the same "destination". My rows will each have an origin and a destination. I need to see all those in which both match. So for instance, if there is a row with Seattle (origin) and Portland (destination) I need to see all other records with Seattle as the origin and Portland as the destination. Additionally, I need to see all records with this type of a match. So if there are records with the same origin and the same destination (not just seattle and Portland), they would also be displayed. Make sense? can you help?

sz1
Aged Yak Warrior

555 Posts

Posted - 2014-03-24 : 12:12:36
How many tables are you querying?

Select t1.origin, t1.destination
From yourtable t1
Where t1.origin = t1.destination

Select t1.origin, t1.destination
From yourtable t1
Left Join yourothertable t2
On t1.ID = t2.ID
Where t1.origin = t2.destination

If you will it you can achieve it!!
Go to Top of Page
   

- Advertisement -