Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Find The Parents, Child and Associated Id in a SQL

$
0
0

Hi,

I have to find the parent, the child and associated Id with which it is grouped in a table.

For example:

ID / Parent ID / Relation1/0 / 02/1 / 13/ 4 / 14/0 / 35/3 / 3

6 / 2/ 2

If I were to input the relation level, Then I will need all the IDs at that level, and their respective parents and child until no more parents or children.

Ex: If Relation = 1 Then output:

ID / Parent / Primary ID

2 /  0 / 2

3 / 4 / 3

5 / 3 / 3

6 / 2 / 2

4 / 0 / 3

I am able to get all the IDs, the child and the parent. But I am not able to pull the primary association ID accurately. 

Can anyone please help?

Thanks.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>