Skip to content
Advertisement

SQL Get Parent when child is column is 0

I have two tables below.

Data:

Using joins I have got the information from both the tables which have needed, but in a new case I need to extract the Address like if the IsNetwork = 0 then Address should of it’s the parent.

Following is the query of join

This is the output of that query:

I want the output as below.

Advertisement

Answer

You need to JOIN to BranchAddress a second time to get the Address of the parent, then select which address to return based on the IsNetwork value of the original row:

Output

Note that the entry for Nariman Point had an invalid ParentId (0) and I’ve changed that to 1 (to be consistent with the expected results) for the demo.

Demo on SQLFiddle

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement