By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,375 Members | 3,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,375 IT Pros & Developers. It's quick & easy.

alternative to distinct on?

P: 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;
Jun 30 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.