Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table groups
contains
GroupID   GroupName ChildId
1           G1        0(0-Means Root Group)
2           G2        0
3           G3        0
4           G4        0
5           G5        1(The GroupId of First Row)
6           G6        5(The GroupId of Fifth Row)
7           G7        2(The GroupId of 2nd Row)
8           G8        7(The GroupId of 7th Row)
9           G9        8(The GroupId of 8th Row)

Now I Want The Group Name Of Root Group
Through Sql query only I want to get
the Name Of the main Group Through its child Id.
For Ex. If I select 9(Group Id) It Is Linked With Main Id-2
9->8->7-2-0(Means Group Name-G2)-Where we will get zero it is over

Thank You
Sukhen Dass
Posted
Updated 10-Feb-10 18:16pm
v5

If you by any chance would be using Oracle the answer would be:
SQL
SELECT  GroupID
FROM    Groups
WHERE   ChildID = 0
START   WITH GroupID = 9
CONNECT BY PRIOR ChildID = GroupID


If you have sqlserver 2008 you can use subquery factoring

SQL
With  t AS (
    SELECT  GroupID,ChildId
    FROM    Groups g
    WHERE   GroupID = 9
        UNION ALL
    SELECT  o.GroupID,o.ChildId
    FROM    Groups g join t
        ON  g.GroupID = t.ChildId
    )
SELECT  t.GroupID
FROM    t
WHERE   ChildId = 0
 
Share this answer
 
v4
You have 3 options.

What you are using is a recursive data structure. It will require that you use recursion or iteration to find the root node. You will need a while loop that repeatedly performs a SELECT statement on the previous node to find it's "child" (in recursive terminology, this would usually be called the "parent", as a child can have one parent but one parent may have many children).

An alternative would be to use a "common table expression" to create a recursive query, though this feature is only available in SQL Server 2008. You can read more about that technique here.

Finally, you can also create a recursive function that basically does what the while loop does, but in a recursive fashion. Note, however, that some versions of SQL Server have limitations on the allowable depth of recursion, though I'm not sure if that is restricted to just stored procedures (you can create a function instead of a stored procedure).
 
Share this answer
 
Nope (I guess): you cannot do that with a SQL query.
I suppose you have to iterate (or possibly recurse).
BTW I'm not a database expert, but you table hardly follows any normalization rule...
:)
 
Share this answer
 
v2
It is tough to do with a single SQL query. Create a stored procedure which takes groupid and recurse on the children. Each iteration will insert row into a temporary table and return the table once done.

I am removing urgent from your post :)
 
Share this answer
 
Sorry my friend

your question is not understood. can you descripb it in more proper way?
 
Share this answer
 

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