Connecting Tech Pros Worldwide Help | Site Map

alternative to distinct on?

  #1  
Old June 30th, 2006, 01:45 PM
robert
Guest
 
Posts: n/a
I'm told "select distinct on" or even "select distinct" is inefficient
because it selects then discards rows.
However, I'm stumped to find an alternative to this query :

select distinct on (user_id) * from bids where auction_id = 3 order by
user_id,created_at DESC;

I'm trying to retreive only the last bid for each user for a given auction.
I'd appreciate any suggestions.


The 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;


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to configure a JBOD disk array (PowerVault) for Performance JRoughgarden answers 2 February 14th, 2006 01:05 AM
Why is a union of two null-results automatically casted to type text ? Frank van Vugt answers 8 November 23rd, 2005 01:10 AM
alternative to frames ian answers 10 November 17th, 2005 06:50 PM
Alternative to self-joins?? nuked@bway.net answers 1 July 23rd, 2005 07:49 AM