Re: Distinct on a non-sort column

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Cstdenis <lists(at)on-track(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Distinct on a non-sort column
Date: 2011-11-05 23:11:31
Message-ID: 17666.1320534691@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Cstdenis <lists(at)on-track(dot)ca> writes:
> If I understand that you are proposing as

> select * from
> (select distinct on (user_id) * from stories as s order
> by user_id) as foo
> order by date_submitted desc limit 10;

No, you always need to sort by *more* columns than are listed in
DISTINCT ON. That's what determines which row is picked in each
DISTINCT group. Read the SELECT reference page's example of how
to use DISTINCT ON.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tatsuo Ishii 2011-11-06 00:17:28 Some services of pgfoundry down?
Previous Message Cstdenis 2011-11-05 21:48:34 Re: Distinct on a non-sort column