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 1mGkwN-00080V-5k for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 16:30:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mGkwM-0000SX-3c for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 16:30:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGkwL-0000SN-R5 for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 16:30:49 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGkwJ-0003uZ-Ro for pgsql-general@postgresql.org; Thu, 19 Aug 2021 16:30:48 +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 17JGUj7l1761172; Thu, 19 Aug 2021 12:30:45 -0400 From: Tom Lane To: "Markhof, Ingolf" cc: pgsql-general@postgresql.org Subject: Re: string_agg distinct order by In-reply-to: References: Comments: In-reply-to "Markhof, Ingolf" message dated "Thu, 19 Aug 2021 18:09:26 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1761170.1629390645.1@sss.pgh.pa.us> Date: Thu, 19 Aug 2021 12:30:45 -0400 Message-ID: <1761171.1629390645@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Markhof, Ingolf" writes: > I am looking for something like > string_agg(distinct col_x order by col_y) > Unfortunately, you can either have the distinct, but then the order by > needs to be identical to what's aggregated, or you can have the order be > determined by another column. Not both... The reason for that restriction is that the case you propose is ill-defined. If we combine rows with the same col_x, which row's value of col_y is to be used to sort the merged row? I think once you answer that question, a suitable query will suggest itself. regards, tom lane