Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ISNULL in self-join

Status
Not open for further replies.

pwills

Technical User
Sep 14, 2002
54
GB
I have a classic self-joinable table: (id, profile, parent_id) where parent_id points to id.

To produce a table of inherited values I have:

select t1.id, case when t2.profile is null t1.profile else t2.profile end
from mytable t1 LEFT JOIN mytable t2
on t2.parent_id = t1.id

Problem is that my table contains 20 profile fields so I would have to have the case statement repeated 20 times.

Can I instead use:

select t1.id, t2.profile
from mytable t1 LEFT JOIN mytable t2
on isnull(t2.parent_id,t1.id) = t1.id
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top