Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1puwO9-00030U-Rx for pgsql-general@arkaria.postgresql.org; Fri, 05 May 2023 14:26:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1puwO8-0006D2-PW for pgsql-general@arkaria.postgresql.org; Fri, 05 May 2023 14:26:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1puwO8-0006Cq-GE for pgsql-general@lists.postgresql.org; Fri, 05 May 2023 14:26:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1puwO5-000kRf-3W for pgsql-general@lists.postgresql.org; Fri, 05 May 2023 14:26:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 345EQIqo3585864; Fri, 5 May 2023 10:26:18 -0400 From: Tom Lane To: "David G. Johnston" cc: Sky Lendar , pgsql-general@lists.postgresql.org Subject: Re: sorting problem with distinct on() In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Fri, 05 May 2023 07:16:22 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3585862.1683296778.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 05 May 2023 10:26:18 -0400 Message-ID: <3585863.1683296778@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Fri, May 5, 2023 at 6:55 AM Sky Lendar 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