Thread: limit results to one row per foreign object
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 alan
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@magproductions.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 //
Without trying it out, how about something like: select username, maxbid from users u, ( select user_id, max(amount) as maxbid from bids group by user_id where auction_id = XXX ) as b where u.id = b.user_id; John 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 > > alan > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly
If you don't mind using a (quite useful) postgres extension, this might work for you: select distinct on (auctions.id, users.id) * from auctions, bids, users where <join clauses> order by auctions.id, users.id, created_at desc; Hope this helps, On Fri, Jun 30, 2006 at 04:13:12PM +0100, 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 > > alan > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly -- Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/ > From each according to his ability. To each according to his ability to litigate.
Attachment
"Alan Bullock" <liststuff@gmail.com> wrote in message news:e83f29$dss$1@sea.gmane.org... > hi all, I have the following schema: > thanks all for such prompt replies! I'm going with Martijn's solution, I'm a total newbie and it appears simplest (though I realise it's pgsql only)