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
 General SQL Server Forums
 New to SQL Server Programming
 Query Same Table Twice

Author  Topic 

superchuck
Starting Member

1 Post

Posted - 2009-09-19 : 08:32:32
The oversimplified version of my issue is this...

Table Locations
[LocationID]
[LocationName]

Table Trips
[TripID]
[TripDate]
[StartingLocation] <--LocationID
[EndingLocation] <--LocationID

You've probably figured it out already, but the question is, how do I do a query that returns all of the trips in the form:

Trips.TripDate, Locations.LocationName, Locations.LocationName

with the starting and ending locations represented.

Thanks!
Chuck

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-09-19 : 09:20:24
SELECT T.TripDate, S.LocationName StartingLocation, E.LocationName EndingLocation
FROM Trips T
INNER JOIN Locations S ON T.StartingLocation=S.LocationID
INNER JOIN Locations E ON T.EndingLocation=E.LocationID
Go to Top of Page
   

- Advertisement -