X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 045E2476CE0 for ; Fri, 16 May 2003 13:25:01 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 00219-08 for ; Fri, 16 May 2003 13:24:58 -0400 (EDT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by postgresql.org (Postfix) with ESMTP id 25336476CC2 for ; Fri, 16 May 2003 13:22:39 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 3.36 #2) id 19Giuj-0007NF-0Z; Fri, 16 May 2003 18:22:37 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 203361682C; Fri, 16 May 2003 18:22:37 +0100 (BST) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 8071E16822; Fri, 16 May 2003 18:22:36 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: sammynash@uboot.com (Sammy), pgsql-sql@postgresql.org Subject: Re: Chain/Thread Problem Date: Fri, 16 May 2003 18:22:37 +0100 User-Agent: KMail/1.4.3 References: <6729af8d.0305160646.d9ab2d2@posting.google.com> In-Reply-To: <6729af8d.0305160646.d9ab2d2@posting.google.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200305161822.37391.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/351 X-Sequence-Number: 13516 On Friday 16 May 2003 3:46 pm, Sammy wrote: > Hi, > > I have a SQL problem for you all. What SQL qurey would I have to make > on the following table to extract the chain (thread) of emails in a > conversation. > > > id|emailid|referenceid > ----------------------- > 1| e125 | Null (Start of chain) > 2| r1fe | e125 (2nd letter in chain) > ... > ... > n| e4ju | r1fe (3rd in chain) Search the archives for "connect by", "hierarchy", "nested" and "celko" - t= his=20 is a limitation of SQL. In your particular case, I'd add a SERIAL "thread_i= d"=20 column which is autogenerated for the first msg in a thread and set to the= =20 first message for all others. You might want to check the contrib/ directory, I've got a feeling there's = an=20 example table-function to do something like this. --=20 Richard Huxton