pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Burton <joel@joelburton.com>
To: Gregory Brauer <greg@wildbrain.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL over my head...
Date: Thu, 16 May 2002 16:43:37 -0400
Message-ID: <JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com> (raw)
In-Reply-To: <3CE418ED.7070103@wildbrain.com>

> -----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=<constant>"
To only look at groups where that's the attribute you want and add "AND
B0.attr_a=<same_constant>" 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




view thread (6+ messages)  latest in thread

Message-ID: <JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com>
Permalink:  ../JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com/
Also on:    postgresql.org/message-id/JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: joel@joelburton.com, greg@wildbrain.com
  Subject: Re: SQL over my head...
  In-Reply-To: <JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox