SQL over my head...

From: Gregory Brauer <greg(at)wildbrain(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: SQL over my head...
Date: 2002-05-16 19:10:11
Message-ID: 3CE40413.8050101@wildbrain.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


I've got a rather difficult query that I'm hoping someone can help with.

I have two tables, foo and bar.

I'll just speak SQL...

CREATE TABLE foo
(
id serial primary key,
attr_a int4 not null,
attr_b int4 not null,
bar_id int4 not null,

unique(attr_a , attr_b, bar_id),

foreign key(bar_id) references bar(id)
);

CREATE TABLE bar
(
id serial primary key,
attr_a int4 not null,
ts timestamp not null
);

What I want to do is find all of the foo.id's where the
foo.bar_id in that row points to a bar where the bar.ts
is the most recent time that is before the current time
among the sets of identical combiniations of foo.attr_a,
foo.attr_b and bar.attr_a.

Said another way...

If I find the 3 item sets of foo.attr_a, foo._attr_b,
and the bar.attr_a that a foo.bar_id points to, and sort
them into groups where the the three values are the same,
I want, as a result, one item from each of the groups
where the time is the maximum of that group which is
still earlier than the current time.

Is this possible? I'd appreciate any help anyone can give.

Thanks.

Greg Brauer
greg(at)wildbrain(dot)com

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2002-05-16 19:11:38 Re: Constraint problem
Previous Message Josh Berkus 2002-05-16 18:48:41 Constraint problem