Re: SQL over my head...

From: "Joel Burton" <joel(at)joelburton(dot)com>
To: "Gregory Brauer" <greg(at)wildbrain(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: SQL over my head...
Date: 2002-05-16 20:43:37
Message-ID: JGEPJNMCKODMDHGOBKDNGEHLCOAA.joel@joelburton.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> -----Original Message-----
> From: Gregory Brauer [mailto:greg(at)wildbrain(dot)com]
> Sent: Thursday, May 16, 2002 4:39 PM
> To: Joel Burton
> Cc: pgsql-sql(at)postgresql(dot)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(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2002-05-16 20:49:18 Re: Constraint problem
Previous Message Stephan Szabo 2002-05-16 20:39:19 Re: Constraint problem