Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1irU2i-00086s-SM for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 21:48:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1irU2h-0004BX-L5 for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 21:48:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1irU2h-0004BQ-8R for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 21:48:07 +0000 Received: from mout.gmx.net ([212.227.17.20]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1irU2e-0000zk-VY for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 21:48:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1579038481; bh=koiPdiildoeActDCK5udlIzbQHj726qBM3fheeZZKoU=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=RtvmpDeH1dAiHk+3WZ5FSrbTqrlC6qIpC6IpAlXqG6691WMqdNMCYMnBHxjAMMpkk N2hsSWKtXWbfIEdc6lnrZG37TNHjbVqd0yEPvn4kFunpah9VqFp0+xecZjq+VAIMz8 dcl3FoUoLE/66G2SWUZPNx9UZnZbFDLsRqq+BKtw= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.20] ([188.174.166.56]) by mail.gmx.com (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MtfJX-1jeuha0XgV-00v6lB for ; Tue, 14 Jan 2020 22:48:01 +0100 Subject: Re: Multiple Aggregations Order To: pgsql-general@lists.postgresql.org References: From: Thomas Kellerer Message-ID: <1c30ad7f-fb8d-e407-8933-9de95dd305d2@gmx.net> Date: Tue, 14 Jan 2020 22:48:00 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:LA8BzfghocZwc+IVxYDDlFvOMz0g+wK4TUYgNbHQcVYUdSCkm6y 3Au4jLHqYAezeifg8aHyU4li0T5usRdt0djoepSU0yOBOAx860bU6VPjX02sMXwK1EUDujp XA5SNxF6z2oWwTFIugSnEtpwuOOB5sqg1a+DUethVckMPfLIt9Mq0DSr4vKI6c6DUvQT54q m4XGI/80JY5muDRd3wSkg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:FaWtc3ZcswA=:CA8yllZVMVMCopk58IbsHq GklIRH5Y1Z+ku+iqxPOtpSGW1+JUalVSac89Thag+yR4DAp4rioocRFO83mZy+JIWiWJlsiSx RqmVru77038XbNnB5hnQxXqsqwQwwPKXs4anmvDnwUnVVfXKBZusheiZn9hlNI5JJil0ZQfg4 +XtWoNhfY9vYYfA0V8dSfCtvky0fcP8LIH1NvKu8ftaGeDp82je51v5CGXbpwJ6s1Kmysl70n 9qg9uynmX1RogIXYsxUZfvV/PbnvwRQn7WdaOxeF02rB70XvlEGtnPZpyRrL00Y2P5E0PxlV1 EgYqUAxUXHT9s+U6IsRusbYqW4ycVzdviZQrcc8phEqMKrnJGh69NGSaERoFaEy9cCOzpgiB9 w+FNe3lKUCSY0IPXju3GZSJ4xVNHtoPQXhLkd2AQipVvwu1eHy8uAqb/9qLJMbz+r/E74rV8i oY2Vcx9ulP3objejz9BW60Y4qVAy/z/byTpcYGY+TK+IhbEUjXgU8IZdX+vtTNa57+/3tnz2A 3Sj9qTmYozpjCd0S11VoBsR6ftnrMVYbSNVycKkmSJMrBdrI1PB9e5Lvlj0huDnoknncvbfGt sQzH7ksUPHVUzZ3YeNSzZvZqJWlmYiA5xsNx2saITkvzgx8FU1Ff5wtp8s+l0wvTHx5vIw6hr OAtE7zVQzH5CkhYWucAM8LDwjrZeIccx0JNAHGE4508Ia1qNhcidLzk1GMCi60Zuocpy89eux KiTmxYenx7yWYSd0rN7kcHNCcH/zBuYZYHz+xwI+OQ5yNukvoexWnqKSSO/OaKsrtPSv4GrNe KZ7IUci9NiYf4OFYxMXAnS08EhwKAQcOf40xHOXyHAp4bcD3mwIHjdXzk8Ww1NroLkC9Q/6mG tfrpQ5KqCMvV/mroLqB0RdcfLNH4eru2G1r4xwzICWCPcNeJLk6xiNRq0zDoJsh+yFqnoNPfH FxSgS4rBMlrdYE0dNbAZO5wWpfVXaNnMpw6HH5FjzNSVAu/1t7nKAfJ/h8J/SDErd9L3D3cba gkuyCC9Ms7jyWxOLsHY4PXECPOfgAWO6xiF6mXsZbW6Gm5aI6L+IqTU8O1hepOgEgOlQJbz5g UcEWUchrj89H0OQqfXdw7dmrtzpwMxT9Yfo/iV4sa9aSyL2CfkRh0w8hWEuILAqbpjCZIYmCx Ii4Qyn5VziZt6aratRQMKD0Tjq/b8TaqgeSUQnUR3xUY8sepL1PzenQHAyAXxOuukCWldXBXK gQgAv/Ki5bNjU+3u/C7HQBU8wersY2pcuSnyOHHT90ZTl0ODEjJmYnA4sGZk= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Jo=C3=A3o Haas schrieb am 14.01.2020 um 18:26: > I'm working on a query where I need to fetch information from a table > along with some data from a many-to-many connection table in a single > query. My idea is to do an outer join with the connection query and > aggregate the needed data in multiple 'array_agg's, and then handle > this aggregated data later in code. > > The issue is, there are a lot of aggs (4 by now, may increase later), > and I need to order these by a 'order' field on the connection table. > I can put an 'ORDER BY "order"' statement inside each 'array_agg', > but I don't think that would be the most efficient way. Doing the > join with a sorted connection table didn't work for me as well, > probably due to other joins on the query. I tried doing some stuff > with subqueries, but all attempts ended up in either failure or > increased query time. > What about aggregating into a single jsonb array? You lose some of the data type information, but maybe that's OK for the ba= ckend that processes the data. Something along the lines: SELECT tb.*, array_length(tree.tree_path, 1) AS depth, jsonb_agg(jsonb_build_object('child_id', conn.child_id, 'kind', = conn.kind, 'restrictions', conn.restrictions) order by conn."order") FROM tb ... GROUP BY ...