Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

MSSQL Select Related Rows

$
0
0

I have a table that has the following (mock) data:

Text
ID | BOB | ORIGINAL_ID
-----------------------
1  | 10  | NULL
2  | NULL| 1
3  | 30  | NULL
4  | 40  | NULL

It's a bit more complex than the sample above, but basically, there are "parent" and "child" records.  The "parent" records are the records that have a value for BOB.  The "child" records are the records that have no value for BOB, but refer back to the "parent" via ORIGINAL_ID.

What I need to do is form a select statement that selects all records, but if the record is a "child" record (ORIGINAL_ID is not NULL), grab the value of BOB from the "parent" record (ORIGINAL_ID of child record = ID of parent record).

Any suggestions on approach is greatly appreciated.  Thanks!!


Viewing all articles
Browse latest Browse all 4871

Trending Articles