Re: SQL over my head... - Mailing list pgsql-sql

From Joel Burton
Subject Re: SQL over my head...
Date
Msg-id JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com
Whole thread Raw
In response to Re: SQL over my head...  (Gregory Brauer <greg@wildbrain.com>)
List pgsql-sql
> -----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



pgsql-sql by date:

Previous
From: Gregory Brauer
Date:
Subject: Re: SQL over my head...
Next
From: Josh Berkus
Date:
Subject: Re: Constraint problem