user4615254
user4615254

Reputation:

SQL Derive multiple columns from one column

I have 1 table called Location, containing columns LocationID, ParentLocationID and LocationName. I want to display a table with "Location" and "Parent Location" both derived from the LocationID. How might I do this ?

Example:

Say for example my table looked like this:

LocationID | ParentLocationID | LocationName

1      |       3          |     abc

2      |       3          |     def

3      |      NULL        |     xyz

I want my returned table to look like:

LocationName | ParentLocationName

 abc         |       xyz
 def         |       xyz

Upvotes: 0

Views: 261

Answers (1)

Robert Columbia
Robert Columbia

Reputation: 6418

Do a self join. For example:

SELECT a.LocationID, a.LocationName,
       parent.LocationID, parent.LocationName
FROM Location a
INNER JOIN Location parent ON a.ParentLocationID=parent.LocationID;

Upvotes: 1

Related Questions