Distinct on a non-sort column

From: Cstdenis <lists(at)on-track(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Distinct on a non-sort column
Date: 2011-11-05 18:39:40
Message-ID: 4EB582EC.7060606@on-track.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am trying to write a query that selects recent submissions (sorted by
submission_date) but only selects the most recent one for each user_id.

example query: /select distinct on (user_id) * from stories order by
date_submitted desc limit 10;/

However postgres will not allow me to filter out duplicate rows with
distinct unless I sort on that column, which would product useless
results for me. Group by seems to have similiar problems, plus the
additional problem of wanting aggregate functions to be used.

I even tried sorting in a subquery, but it still comes out sorted by
user_id: /select distinct on (user_id) * from stories where sid in
(select sid from stories order by date_submitted desc limit 10);/

How can I work around this limitation to get the results sorted the way
I want, then have the duplicates removed? I can't be the only one
running into this limitation, there must be some workaround.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2011-11-05 18:55:02 Re: What is *wrong* with this query???
Previous Message Martijn van Oosterhout 2011-11-05 18:20:23 Re: Strange problem with create table as select * from table;