From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Sky Lendar <skylendar@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: sorting problem with distinct on()
Date: Fri, 05 May 2023 10:26:18 -0400
Message-ID: <3585863.1683296778@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwaa8z-=2S7A1KBez4ga=k02SdYffVtkca3YXrYQFQFD+w@mail.gmail.com>
References: <CAJYOnMsrKKfQxwDZYiZx=a6xDruYCeQy+ZZPC+WgaFCdR1z1zA@mail.gmail.com>
<CAKFQuwaa8z-=2S7A1KBez4ga=k02SdYffVtkca3YXrYQFQFD+w@mail.gmail.com>
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Fri, May 5, 2023 at 6:55 AM Sky Lendar <skylendar@gmail.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, skylendar@gmail.com
Subject: Re: sorting problem with distinct on()
In-Reply-To: <3585863.1683296778@sss.pgh.pa.us>
References: <CAJYOnMsrKKfQxwDZYiZx=a6xDruYCeQy+ZZPC+WgaFCdR1z1zA@mail.gmail.com> <CAKFQuwaa8z-=2S7A1KBez4ga=k02SdYffVtkca3YXrYQFQFD+w@mail.gmail.com> <3585863.1683296778@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox