I need to figure out the best way, in SQL, to query on a Parent / Child relationship. Some of the parent fields will be the data for the child. Here is an example:
ID Field1 Field2 ParentId
--------------------------------------------
1 stuff moreStuff 0
2 childStuff (from parent) 1
So, Field2 for a child would be the value of whatever the parent’s is. I need to figure out how to write my SQL so that when the records get pulled back Field@ for the child would be “moreStuff”. I am using SQL server 2008.
Thank you.
Assuming Field2 cannot be NULL, you could use a LEFT JOIN with COALESCE:
If that Field2 can be NULL, replace the coalesce expression with the following: