Received: from bjke.com (firewall-user@ganymede.bjke.com [144.210.8.38]) by hub.org (8.9.2/8.9.1) with ESMTP id QAA67642 for ; Wed, 3 Mar 1999 16:46:55 -0500 (EST) (envelope-from djackson@cpsgroup.com) Received: by bjke.com; id PAA14111; Wed, 3 Mar 1999 15:46:51 -0600 (CST) Received: from cpsmail.cpsgroup.com(144.210.12.11) by gauntlet.bjke.com via smap (4.1) id xmac13490; Wed, 3 Mar 99 15:46:22 -0600 Received: by cpsmail with Internet Mail Service (5.0.1458.49) id ; Wed, 3 Mar 1999 15:49:04 -0600 Message-ID: From: "Jackson, DeJuan" To: PGSQL SQL Subject: FW: Trees in SQL Date: Wed, 3 Mar 1999 15:49:02 -0600 X-Priority: 3 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.0.1458.49) Content-Type: text/plain -----Original Message----- From: Jackson, DeJuan Sent: Wednesday, March 03, 1999 1:18 PM To: 'Bartek Teodorczyk' Subject: RE: Trees in SQL > I'm trying to implement set model of trees in SQL and I found > a problem: > > How to find first level children of parent? > > For this structure: > > Jerry > | > ------------------- > | | > Bert Chuck > | > ------------ > | | > Donna Fred > > The question is: Who is first level child of Jerry? > The answer should produce: > Bert > Chuck > > Maybe you know how to formulate the query? > > BarTeo Assuming the above tree structure we have: l | r | data ------------ 1 |10 | Jerry 2 | 3 | Bert 4 | 9 | Chuck 5 | 6 | Donna 7 | 8 | Fred What we need to do is find all children of Jerry that isn't a child of anyone else who is a child of Jerry. SELECT p1.* FROM people p1, people p2 WHERE p2.lp1.r AND p2.data = 'Jerry' AND NOT EXISTS(SELECT 1 FROM people p3 WHERE p3.lp1.r AND p2.lp3.r) Results: l|r|data -+-+----- 2|3|Bert 4|9|Chuck Hope this helps, -DEJ