From: | Alban Hertroys <alban(at)magproductions(dot)nl> |
---|---|
To: | Alan Bullock <liststuff(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: limit results to one row per foreign object |
Date: | 2006-06-30 15:33:42 |
Message-ID: | 44A54456.9070002@magproductions.nl |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Alan Bullock wrote:
> hi all, I have the following schema:
>
> CREATE TABLE auctions ( id serial NOT NULL, user_id int4, title
> varchar(255), body varchar(255), CONSTRAINT auctions_pkey PRIMARY KEY (id) )
> WITHOUT OIDS;
>
> CREATE TABLE bids ( id serial NOT NULL, auction_id int4, user_id int4,
> amount float8, created_at timestamp, CONSTRAINT bids_pkey PRIMARY KEY (id) )
> WITHOUT OIDS;
>
> CREATE TABLE users ( id serial NOT NULL, username varchar(255), CONSTRAINT
> users_pkey PRIMARY KEY (id) ) WITHOUT OIDS;
>
> I'd like to return all the bids for a given auction, but limit it to only
> the *latest* bid from each user. so regardless of how many bids a user has
> placed, only their latest is returned.
>
> I dont have a clue where to even start with this and would appreciate some
> pointers
> thanks
Hmm... No foreign key constraints?
I think you're looking for something like:
SELECT *
FROM bids
WHERE auction_id = 1234
GROUP BY user_id
HAVING created_at = MAX(created_at);
You could also use a subselect with an order by created_at DESC limit 1
over each users bids.
Regards,
--
Alban Hertroys
alban(at)magproductions(dot)nl
magproductions b.v.
T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede
// Integrate Your World //
From | Date | Subject | |
---|---|---|---|
Next Message | Alban Hertroys | 2006-06-30 15:36:14 | Re: Notes on converting from MySQL 5.0.x to PostgreSQL |
Previous Message | Dave Page | 2006-06-30 15:22:28 | Re: Notes on converting from MySQL 5.0.x to PostgreSQL |