Search Postgresql Archives

Re: Distinct on a non-sort column

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Cstdenis <lists@xxxxxxxxxxx> writes:
> 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.

Do the DISTINCT ON in a sub-query, with an ORDER BY appropriate for that
task, and then re-sort the rows the way you want them presented in the
outer query.

	SELECT ... FROM
		(SELECT DISTINCT ON ... ORDER BY ...) ss
	ORDER BY ...;

			regards, tom lane

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux