Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

alternative to distinct on?

Question posted by: robert (Guest) on June 30th, 2006 12:45 PM
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;


 
Not the answer you were looking for? Post your question . . .
190,071 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors