Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, I have a table "Demo" which has following record,
GroupId     GroupName                                          ParentGroupId IsADGroup
----------- -------------------------------------------------- ------------- ---------
1           Default                                            NULL          0
6           Hs-First Floor                                     1             0
7           Hs-First Floor-1                                   6             0
8           Hs-Second Floor                                    6             0
9           Hs-Third Floor                                     6             0
10          Hs-Second Floor-1                                  8             0
11          Hs-Second Floor -1-1                               10            0
In these while editing I want to get those record whose group id & parent group id is
not in that record.

for Example: Suppose I want to edit Hs- first Floor (Group Id=6) in that case Based on that id,
I want to show a record whose parentid & group id is not a "6" and also I don't want to show group id="10" & "11" .because his parent id="8 and 10", and "8 and 10 has parent id=6."
So, here expected result should be 1 Default NULL 0
Posted
Updated 6-May-13 0:56am
v2
Comments
Ali Reza Barkhordari 6-May-13 6:44am    
you must use a recursive query to do this.

1 solution

Hello Vaibhav,

Adding a recursion will be one way..

i tried to create a solution which will avoid recursion as below

SQL
SELECT DISTINCT A.* from Demo A LEFT JOIN DEMO B ON A.GroupID !=  B.ParentGroupId
WHERE ISNULL(A.ParentGroupId,-99) != 6 AND ISNULL(A.ParentGroupId,-99) NOT IN ( SELECT DISTINCT A.GroupID from Demo A LEFT JOIN DEMO B ON A.GroupID =  B.ParentGroupId WHERE B.ParentGroupId !=6)


Hope that it should solve your problem(i know it looks weird but for sake of avoiding recursion it can be used)

Please mark it as answer if this seems correct to you
 
Share this answer
 
Comments
vaibhav10Dec1987 7-May-13 2:23am    
Hi, Thanks for your help,
But In that case it works upto second level. Here i want to eliminate that group itself
as well as all it's directly dependent as well as indirectly dependent upto n-level.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900