Below is what I have in my table.
++++++++++++++++++++
Parent + Child
++++++++++++++++++++
C1 + G1
C1 + G2
C1 +
first Create Recursive function for calc level.
function fn_CalcLevel(int @ID)
As Begin
Declare @ParentID int
Select @ParentID = ParentID From Table1 where ID = @ID
IF (@ParentID IS NULL) Return 1 Else Return 1+fn_CalcLevel(@ParentID)
End
then Create your Query Such as Below
Select *, fn_CalcLevel(Table1.ID) as Level
From Table1