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 1siucW-00AwBL-N1 for pgsql-sql@arkaria.postgresql.org; Tue, 27 Aug 2024 11:44:20 +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 1siucU-006Rfc-Q8 for pgsql-sql@arkaria.postgresql.org; Tue, 27 Aug 2024 11:44:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1siucU-006RfU-Cs for pgsql-sql@lists.postgresql.org; Tue, 27 Aug 2024 11:44:19 +0000 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1siucS-001gAO-1o for pgsql-sql@lists.postgresql.org; Tue, 27 Aug 2024 11:44:17 +0000 Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-e165825ebfdso5326368276.0 for ; Tue, 27 Aug 2024 04:44:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lelarge-info.20230601.gappssmtp.com; s=20230601; t=1724759054; x=1725363854; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Eolp99sTxQ7YA0ZmljnCHdJq9XcYAvxAGgxaMoCIx/U=; b=idS+A+1/TH19QTu3wWLvU/RVfgWgCeBqJx7b3S+rx4uAbl46c016Q4YBU9E8DMTGSk WVgiHKnAwEvD1VLrXCUUqTBaBvIOk1YMAeMhuVmBi/y7KgJcwpcjxRYaQxqvSAL8BY3i CzkVjeh3GOOYyTkv6cj1u954Os64zow8z673wdr2zAmxp7GJdrrPzpYQFdCydqZ/ijOB qr2V8rd/hvxJeiOH/g3RSEekRQWyyTzUQ0zieKJNQ/Qz4kWLxWO8Fu9BswEvooxkknu9 DnciW4hrKNITVvZXYqKgK+f54d5vXt84wSAp3KCiwCEloJb2TJRJXCLUQLE25i11VPLo 4hgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724759054; x=1725363854; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Eolp99sTxQ7YA0ZmljnCHdJq9XcYAvxAGgxaMoCIx/U=; b=GBn17OomzUBx37+e8ea0KG/T5khWhuuwYIv4rJlwxMRq9JByrktJZsJMuis3O9ICiA Hi/p6PWE22/4yF3rjiDdACv3uoA94hsBqC/6eYH4uNbSh0LGZbO0KHKmYO6HPT8/2jqz 29otcv0aud9lkQGJIRbv1umFQUrCG/ORORKcwabQPRKYDxiw9eYcCuQpclxHTsq9SBHD eZkP1aDsjj42yUDvBOo0xlPcpy0uOy0Lg0cVxp+RgGOAz+gLU8cv170k0MVRIPvRX7JP Y43JdPTFVKVE/twGYNEW3yUZKmoh3bTEwLl3gOwqQZ9xtHsvBrSBCADnHkQ3YAZzipXl eWCA== X-Gm-Message-State: AOJu0YwWFXVFpfD4GhBbLdxsdAgPYSFHp8n7CtKoDZHjmkpV82u/NZPL iAlZvAB5eYjoemfUaHjCPahRktT9xt5H1IpKkPGFIkPy1MKmtQgczZ/y9HQGjTxsZC9DOb2C1gR Z1mwqn3/lK7BxYEEuWnPdC09A9q8PSDa4Hz6bVhknkE/gErdD X-Google-Smtp-Source: AGHT+IGzzt8Mj0Fxfl6BK+67TfkfBDE483eLphwUQVmRiIdMgVXOpMtiypC9ZN/MxSnvDqWil9Q7YzKU2jGTBmef/Qw= X-Received: by 2002:a05:6902:1501:b0:e11:6348:5d95 with SMTP id 3f1490d57ef6-e1a2a5a76a5mr2570953276.7.1724759054261; Tue, 27 Aug 2024 04:44:14 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Guillaume Lelarge Date: Tue, 27 Aug 2024 13:44:02 +0200 Message-ID: Subject: Re: ORDER BY elements in ARRAY To: Andreas Joseph Krogh Cc: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000c0bca70620a8c202" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c0bca70620a8c202 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, Le mar. 27 ao=C3=BBt 2024 =C3=A0 13:23, Andreas Joseph Krogh a =C3=A9crit : > Hi, I have this query: > > > > SELECT > comp.entity_id as company_id, comp.companyname AS company_name > , ARRAY( > SELECT > (pers.entity_id > , pers.firstname > , pers.lastname > )::PersonTypeTest > FROM onp_crm_person AS pers > WHERE pers.relation_id =3D comp.entity_id > ) as person_array > > FROM onp_crm_relation comp > ORDER BY person_array > ; > > > > And I'd like to ORDER BY (conceptually) lower(pers.firstname), > lower(pers.lastname). I realize there might be more than 1 person in the > array, so I'd like to order by =E2=80=9Cfirstname and lastname of first e= lement, > the same for second etc.=E2=80=9D > > > > Any way to do this? > > array_agg might be what you're looking for, but you'll have to extract th= e subquery, and transform it to a join on the main query. Regards. --=20 Guillaume. --000000000000c0bca70620a8c202 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

Le=C2=A0mar. 27 ao=C3=BBt 2024 =C3= =A0=C2=A013:23, Andreas Joseph Krogh <andreas@visena.com> a =C3=A9crit=C2=A0:

Hi, I have this query:

=C2= =A0

SELECT
    comp.entity_id as company_id, comp.companyname AS company_name
     , ARRAY(
        SELECT
            (pers.entity_id
                , pers.firstname
                , pers.lastname
                )::PersonTypeTest
        FROM onp_crm_person AS pers
        WHERE pers.relation_id =3D comp.entity_id
       ) as person_array

FROM onp_crm_relation comp
ORDER BY person_array
;

=C2=A0

And I'd like to ORDER BY (conceptually) l= ower(pers.firstname), lower(pers.lastname). I realize there might be more t= han 1 person in the array, so I'd like to order by =E2=80=9Cfirstname a= nd lastname of first element, the same for second etc.=E2=80=9D

=C2= =A0

Any way to do this?

array_agg m= ight be what you're looking for, but you'll have to extract the sub= query, and transform it to a join on the main query.

Regards.


--
Guillaume.
<= /div>
--000000000000c0bca70620a8c202--