How to build a query of recursive query in sql for getting the leaf node to root node

Viewed 42

Below SQL table is called FriendsInformation and it contains columns Name and FriendOf.

enter image description here

Here i want to form a query in such a way that i get the below output

select Name , { Code } as FriendPath from FriendsInformation where Name = 'Person7'

enter image description here

The query should fetch like above output where FriendPath column will fetch the parent ones friend till the FriendOf column is null.

suppose if we want to fetch Person7, i need to get all the hierarchies of friendship so that friendship path is identified

Whether With CTE would help or Do While loop help i am not sure about it. I just tried inner joins and CTE's but could not able to get the value , Could anyone assist on this ?

0 Answers
Related