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 1pY0or-00058c-3g for pgsql-general@arkaria.postgresql.org; Fri, 03 Mar 2023 08:31:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pY0oq-0003gA-24 for pgsql-general@arkaria.postgresql.org; Fri, 03 Mar 2023 08:31:12 +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 1pY0op-0003g1-MI for pgsql-general@lists.postgresql.org; Fri, 03 Mar 2023 08:31:11 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pY0ol-0000i4-D1 for pgsql-general@lists.postgresql.org; Fri, 03 Mar 2023 08:31:11 +0000 Received: by mail-ed1-x52f.google.com with SMTP id cw28so7246918edb.5 for ; Fri, 03 Mar 2023 00:31:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677832266; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Il6z2xlNVtu/taws6gQ1MQdsqq8PmXXiIFygEs4Lt6A=; b=k6/+qUo3DCh1AC9l3LTh2mohnq2Z6SYdhXo/U28RMh7XusoU5R1a9kAOY0TyLAzRS/ /SNtGrUVe8PZKPfU90C+7//z8OFgJ+8VTcOuWv1TVOEaBt9SJpv0TVc5fMkbu3BCYacW SHf05ljzrNDRbi4PXp8Q2z9CVNzSpD6aB5yaB/CuvqK0z4hCDISNRp4CiBSt1UQn8RTj f8FVE0dorYGN7iPZSvGwC23J5RTnB2v27IY+EFi/5zMmsYtDRzeQ5r3edJME58KHOq77 w+C0rAz/u8F/U7rSFaaqPYKG+vucT4MWg8zoto2Btqz+unxIX1SQSouPpAtWCIxmp6lf E45A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677832266; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Il6z2xlNVtu/taws6gQ1MQdsqq8PmXXiIFygEs4Lt6A=; b=2jCs4SOtiKlZdZmsqQwIBRhgnOVdd2KhgrwuXKQYIrWGfuY5g19J8/6aGW4iEyDO5R LIfdYkb+wRaZnkKBCO88u1jpZec5FyLX/ck7dOpnAVKwLTuWEsRjAoYyRFlY79tLkvl+ 3iK/i6QMBSrJzmQ2c6EEKO+YSXKZ/T2LmtP5MNlL4CK0nNPyrOs98EVP4hpxcDNFKK4D c9g2TKcq+mfI6p26XD4s5O9wKQRdsTmbKSZ49fx4Hhm6DOsWAiu21jb/oERf1epu2DyL JmckHh4XTSE89Nvb/v8nVY+YqCE62SuQYvSLzUSI0u5VFDrAwqL2mSKo+5r1kIasAhIE yLeQ== X-Gm-Message-State: AO0yUKUSOI0C+PwT4O8pm2cCkERNgvfiRhYoBwFWuPqne9V1rfWBWoUi LddMKBeCXNEqScpcb8N2DVc= X-Google-Smtp-Source: AK7set8Yq68FiW4ybmUjl4FE9kKoYXMaLrMIegH6+poqTfg/uEozKL/8taVAuj+Zq9U9beHfjIApUw== X-Received: by 2002:a17:907:8c09:b0:88a:888b:c8b with SMTP id ta9-20020a1709078c0900b0088a888b0c8bmr1032420ejc.35.1677832266258; Fri, 03 Mar 2023 00:31:06 -0800 (PST) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id h30-20020a50cdde000000b004c10b4f9ebesm866627edj.15.2023.03.03.00.31.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Mar 2023 00:31:05 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!)y From: Alban Hertroys In-Reply-To: <9da4cb5a-81bb-23a4-8261-6ba8a29579af@evolvis.org> Date: Fri, 3 Mar 2023 09:31:03 +0100 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <5AF0C3F0-4D5A-4198-B58B-6E7D4994969B@gmail.com> References: <23d08470-aaa8-afca-dfd7-37261487a1f8@evolvis.org> <559416.1677547978@sss.pgh.pa.us> <9fc8e12b-f943-c633-a37-254915f819a0@evolvis.org> <9da4cb5a-81bb-23a4-8261-6ba8a29579af@evolvis.org> To: Thorsten Glaser X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 3 Mar 2023, at 0:02, Thorsten Glaser wrote: >=20 > On Tue, 28 Feb 2023, Alban Hertroys wrote: >=20 >> Perhaps you can use a lateral cross join to get the result of >> jsonb_build_object as a jsonb value to pass around? >=20 > I don=E2=80=99t see how. (But then I=E2=80=99ve not yet worked with = lateral JOINs.) You posted this bit: > jsonb_build_object('opening_times', > jsonb_agg(DISTINCT jsonb_build_object( > 'weekday', cot.weekday, > 'from_hour', cot.from_hour, > 'to_hour', cot.to_hour) > ORDER BY > jsonb_build_object( > 'weekday', cot.weekday, > 'from_hour', cot.from_hour, > 'to_hour', cot.to_hour)->>'weekday', > jsonb_build_object( > 'weekday', cot.weekday, > 'from_hour', cot.from_hour, > 'to_hour', cot.to_hour)->>'from_hour', > jsonb_build_object( > 'weekday', cot.weekday, > 'from_hour', cot.from_hour, > 'to_hour', cot.to_hour)->>'to_hour') > ) You can rewrite that into something like this: select jsonb_build_object('opening_times=E2=80=99, obj ORDER BY obj->>'weekday=E2=80=99, obj->>'from_hour=E2=80=99, obj->>'to_hour') ) from cot cross join lateral jsonb_agg(jsonb_build_object( 'weekday', cot.weekday, 'from_hour', cot.from_hour, 'to_hour', cot.to_hour) obj That=E2=80=99s off the top of my head and I did leave out the DISTINCT. = Just to show the concept here. A bit of experimenting and reading should = get you there, I=E2=80=99m keeping $work waiting :P (=E2=80=A6) > WITH > cgwaj AS ( > SELECT cgwa.id AS id, jsonb_build_object( > 'weekday', cgwa.weekday, > 'forenoon', cgwa.forenoon, > 'afternoon', cgwa.afternoon, > 'evening', cgwa.evening) AS obj > FROM core_generalworkavailability cgwa > ), > -- =E2=80=A6 same for opening times > SELECT cp.email, =E2=80=A6, > -- =E2=80=A6 > jsonb_build_object('possible_work_times', COALESCE( > jsonb_agg(DISTINCT cgwaj.obj ORDER BY cgwaj.obj->>'weekday', > cgwaj.obj->>'forenoon', cgwaj.obj->>'afternoon', > cgwaj.obj->>'evening') > FILTER (WHERE cgwaj.id IS NOT NULL))) || > -- =E2=80=A6 > FROM core_person cp > -- =E2=80=A6 > LEFT JOIN core_person_possible_work_times cppwt ON = cppwt.person_id=3Dcp.id > LEFT JOIN cgwaj ON cgwaj.id=3Dcppwt.generalworkavailability_id > -- =E2=80=A6 >=20 > That is, add a CTE for each m:n-attached table whose =E2=80=9Cvalue=E2=80= =9D is > an object, not a single field, keep the id field; LEFT JOIN that > (instead of the original table), then we have a field to use in > ORDER BY. >=20 > I think. I=E2=80=99ve not yet tried it (I don=E2=80=99t have access to = that DB > normally, I was just helping out). >=20 > This avoids sub-SELECTs in the sense of needing to run one for > each user row, because the innermost JSON object building needs > to be done for each (connected (if the query is not filtering on > specific users)) row of the =E2=80=9Cproperty table=E2=80=9D, anyway. = (And even > if filtered, that can be passed down.) >=20 > bye, > //mirabilos > --=20 > Solange man keine schmutzigen Tricks macht, und ich meine *wirklich* > schmutzige Tricks, wie bei einer doppelt verketteten Liste beide > Pointer XORen und in nur einem Word speichern, funktioniert Boehm ganz > hervorragend. -- Andreas Bogk =C3=BCber boehm-gc in d.a.s.r >=20 Alban Hertroys -- There is always an exception to always.