Connecting Tech Pros Worldwide Forums | Help | Site Map

alternative to distinct on?

robert
Guest
 
Posts: n/a
#1: Jun 30 '06
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