Get A Tree from a table

From: gzscott2001(at)yahoo(dot)ca (Scott Yaung)
To: pgsql-sql(at)postgresql(dot)org
Subject: Get A Tree from a table
Date: 2002-10-06 05:37:05
Message-ID: 341834e1.0210052137.7cbd6342@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I like to do something like this:(build a tree from relationship)


I have a table like this

NODES__________________________________
ParentID Varchar(20)
ParentType varchar(20)
ChildID varchar(20)
ChildType varchar(20)

__________________________________
What in the table
'A1', 'A', 'B1', 'B'
'A2', 'A', 'B2', 'B'
'A1', 'A', 'B3', 'B'
'B1', 'B', 'C1', 'C'
'B1', 'B', 'C2', 'C'
'C1', 'C', 'D1', 'D'
'A1', 'A', 'B4', 'B'
'B1', 'B', 'C5', 'C'

----------------------------------

now I like to get all nodes with 'A1' as root
to get the result like this
'A1', 'A', 'B1', 'B'
'B1', 'B', 'C1', 'C'
'C1', 'C', 'D1', 'D'
'B1', 'B', 'C2', 'C'
'B1', 'B', 'C5', 'C'
'A1', 'A', 'B3', 'B'
'A1', 'A', 'B4', 'B'

or I just like to get another tree start with 'B1'
like this
'B1', 'B', 'C1', 'C'
'C1', 'C', 'D1', 'D'
'B1', 'B', 'C2', 'C'
'B1', 'B', 'C5', 'C'

How can i make it by sql , and sql functions
Thanks lot and regards.

Scott

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message pete 2002-10-06 11:13:41 Re: problem with subqueries
Previous Message Stephan Szabo 2002-10-06 01:02:09 Re: problem with subqueries