Pages

Social Icons

Tuesday 6 November 2012

LEFT JOIN with same table

Today i come across a situation where my friend want to show parent column value near to child calumn so he is writing a subquery there. I write the following query

FROM 

TabelA As Parent
Left JOIN
TabelA As Child
On Parent.Id = Child.ParentID

After this query he is getting the perfect result. Avoid the subquery whereever it is possible because it hits the performance badly.


After this query he is writing case statement to append the parent column value in child calumn. I have replace that case statement with followiing


SELECT

Child.Value + ' ' + ISNULL(Parent.Value,'')
FROM 
TabelA As Child
Left JOIN
TabelA As Parent
On Parent.Id = Child.ParentID

Always try to use the SQL in-built function

No comments:

Post a Comment