Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sMBjP-0010m1-FA for pgsql-general@arkaria.postgresql.org; Tue, 25 Jun 2024 19:21:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sMBjN-0003VS-Ji for pgsql-general@arkaria.postgresql.org; Tue, 25 Jun 2024 19:21:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sMBjN-0003VJ-8m for pgsql-general@lists.postgresql.org; Tue, 25 Jun 2024 19:21:29 +0000 Received: from wolff.to ([98.103.208.27]) by magus.postgresql.org with smtp (Exim 4.94.2) (envelope-from ) id 1sMBjL-003WwM-08 for pgsql-general@lists.postgresql.org; Tue, 25 Jun 2024 19:21:28 +0000 Received: (qmail 3642 invoked by uid 500); 25 Jun 2024 19:11:57 -0000 Date: Tue, 25 Jun 2024 14:11:57 -0500 From: Bruno Wolff III To: Tom Lane Cc: pgsql-general@lists.postgresql.org Subject: Re: Can any_value be used like first_value in an aggregate? Message-ID: <20240625191157.GA2861@wolff.to> Mail-Followup-To: Bruno Wolff III , Tom Lane , pgsql-general@lists.postgresql.org References: <20240625164208.GA1043@wolff.to> <1337453.1719335325@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <1337453.1719335325@sss.pgh.pa.us> User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Jun 25, 2024 at 13:08:45 -0400, Tom Lane wrote: > >Not really. It will work that way in simple cases, but I think the >behavior stops being predictable if the input gets large enough to >induce the planner to use parallel aggregation. In any case, the >example shown isn't amazingly efficient since it'll still perform >a sort to meet the ORDER BY spec. Thanks. >> The use case is that I want to return a value of one column that is paired >> with the maximum value of another column in each group when using GROUP BY. > >Use window functions (i.e. first_value). This is what they're for, >and they are smart enough to do just one sort for functions sharing >a common window spec. If I do that, I'd need to do it as a subselect inside of a group by. I'm thinking distinct on may work and be a better way to do it. The actual use case is a set of tripplets returned from a query, where I want on row for each distinct value in the first column, paired with the value in the second column, for which the third column is the largest. The second and third columns are effectively dependent on each other, so there won't be any ambiguity. Thanks for getting me thinking about some other ways to approach the problem.