Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 494A947650E for ; Thu, 16 May 2002 16:49:03 -0400 (EDT) Received: from temp.joelburton.com (unknown [209.190.238.101]) by postgresql.org (Postfix) with ESMTP id 3EF6F47616E for ; Thu, 16 May 2002 16:43:36 -0400 (EDT) Received: from joeltf0d13cyyp (unknown [209.190.238.100]) by temp.joelburton.com (Postfix) with SMTP id 25ACE2B981; Thu, 16 May 2002 16:48:58 -0400 (EDT) From: "Joel Burton" To: "Gregory Brauer" Cc: Subject: Re: SQL over my head... Date: Thu, 16 May 2002 16:43:37 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) In-Reply-To: <3CE418ED.7070103@wildbrain.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Archive-Number: 200205/177 X-Sequence-Number: 7812 > -----Original Message----- > From: Gregory Brauer [mailto:greg@wildbrain.com] > Sent: Thursday, May 16, 2002 4:39 PM > To: Joel Burton > Cc: pgsql-sql@postgresql.org > Subject: Re: [SQL] SQL over my head... > > > > SELECT F0.id > > FROM Foo as F0 > > JOIN Bar as B0 ON (F0.id=B0.id) > > WHERE NOT EXISTS ( > > (SELECT * > > FROM Foo AS F1, > > Bar as B1 > > WHERE ts < CURRENT_TIME > > AND F0.attr_a=F1.attr_a > > AND F0.attr_b=F1.attr_b > > AND B0.attr_a=B1.attr_a > > AND B1.ts > B0.ts) > > > Wow, thanks for the help! I'm still parsing this, but one comment > and one question... > > First, I realized that though I will probably need this > more general case later, in what I am doing right now, I know > a single bar.attr_a value that I want, so only foo.attr_a and > foo.attr_b are variable. Does that change anything? If I'm understanding correctly, just make the line "AND B1.attr_a=" To only look at groups where that's the attribute you want and add "AND B0.attr_a=" at the end of the query (after the closing paren) to make that count for the outer query, too. > Secondly, in the third line, should (F0.id=B0.id) actually be > (F0.bar_id=B0.id) to join on the foreign key? Yes, it should. - J. Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton Knowledge Management & Technology Consultant