Unix Technical Forum

query to trace all parents

This is a discussion on query to trace all parents within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I have a table with filled out below data: +------+-----+ |parent|child| +------+-----+ |A |B | |B |C | ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 08:09 AM
PYCTAM
 
Posts: n/a
Default query to trace all parents

Hi,

I have a table with filled out below data:

+------+-----+
|parent|child|
+------+-----+
|A |B |
|B |C |
|B |E |
|C |D |
|E |F |
|E |G |
+------+-----+

So I have to make a query which get all 'parent' values values for
given child value.

For example :
-----------------
If I have to get all parent values for 'D' child., query must get this
values : C, B, A.

If I have to get all parent values for 'F' child., query must get this
values : E, B, A.

If I have to get all parent values for 'C' child., query must get this
values : B, A.

If I have to get all parent values for 'B' child., query must get this
values : A only.
-----------------

Is it possible to create a query which will covers all above conditions
or not using only sql statement without UDF or stored procedures.

Any solutiuons?

Sincerely,
Rustam Bogubaev

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 08:09 AM
Erland Sommarskog
 
Posts: n/a
Default Re: query to trace all parents

PYCTAM (rbogubaev@bookinturkey.com) writes:
> So I have to make a query which get all 'parent' values values for
> given child value.
>
> For example :
> -----------------
> If I have to get all parent values for 'D' child., query must get this
> values : C, B, A.
>
> If I have to get all parent values for 'F' child., query must get this
> values : E, B, A.
>
> If I have to get all parent values for 'C' child., query must get this
> values : B, A.
>
> If I have to get all parent values for 'B' child., query must get this
> values : A only.
> -----------------
>
> Is it possible to create a query which will covers all above conditions
> or not using only sql statement without UDF or stored procedures.


In SQL2000, no.

In SQL 2005, slated for release in November, there is support for
recursive queries.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 08:10 AM
PYCTAM
 
Posts: n/a
Default Re: query to trace all parents

in this hopeless situation problem is solved using UDF :

CREATE FUNCTION getAllParents(
@child NVARCHAR(1)
) RETURNS @PARENTS TABLE (
[parent] NVARCHAR(1)
) AS BEGIN
DECLARE @parent NVARCHAR(1)

SELECT @parent = parent FROM table WHERE child = @child

WHILE @@ROWCOUNT = 1 BEGIN
INSERT @PARENTS SELECT @parent

SELECT @child = @parent
SELECT @parent = parent FROM table WHERE child = @child
END

RETURN
END

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 08:10 AM
--CELKO--
 
Posts: n/a
Default Re: query to trace all parents

Again, get a copy of TREES & HIERARCHIES IN SQL and look up the Nested
Sets Model for trees.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:57 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com