Re: sorting problem with distinct on()

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Sky Lendar <skylendar(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: sorting problem with distinct on()
Date: 2023-05-05 14:26:18
Message-ID: 3585863.1683296778@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> writes:
> On Fri, May 5, 2023 at 6:55 AM Sky Lendar <skylendar(at)gmail(dot)com> wrote:
>> Notice that 12 is missing in the list.

> Since your DISTINCT ON *subquery* doesn't specify an ordering which of
> those two are chosen as the representative record for M31 is
> non-determinstic.

> If you want to ensure the lowest valued nb is chosen you need to sort the
> *subquery*.

The "weather_reports" example here might help clarify this for you:

https://www.postgresql.org/docs/current/sql-select.html

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2023-05-05 15:06:29 Re: SSL Enablement in Postgres via Client App like PG-ADMIN-4, Java.
Previous Message Sky Lendar 2023-05-05 14:25:18 Re: sorting problem with distinct on()