Sv: ORDER BY custom type

From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Sv: ORDER BY custom type
Date: 2018-01-11 12:51:21
Message-ID: VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

På torsdag 11. januar 2018 kl. 12:00:55, skrev Andreas Joseph Krogh <
andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com>>:
Hi all.
 
I have this custom-type:
 
CREATE TYPE PERSONTYPE AS ( id BIGINT, firstname VARCHAR, lastname VARCHAR,
initialsVARCHAR );
 
I'm returning a column with this type using the following query, which lists
activities and its responsible-person (using the PERSONTYPE):
SELECT act.entity_id AS project_id, act.name AS project , (SELECT ROW
(p.entity_id, p.firstname, p.lastname, p.initials)::persontypeFROM
onp_crm_person pWHERE act.responsible = p.onp_user_id) AS responsible_person
fromonp_crm_activity act ORDER BY responsible_person ASC ;
 
But I need to ORDER BY different properties of PERSONTYPE, ie. initials, and
am wondering if there exists a mechanism to achieve the following:
SELECT act.entity_id AS project_id, act.name AS project , (SELECT ROW
(p.entity_id, p.firstname, p.lastname, p.initials)::persontypeFROM
onp_crm_person pWHERE act.responsible = p.onp_user_id) AS responsible_person
fromonp_crm_activity act ORDER BY responsible_person.initials ASC ;
How do I do that?
 
Thanks.
 
I see that if I wrap the query in an outer query I can do it like this:
SELECT * FROM ( SELECT act.entity_id AS project_id, act.name AS project , (
SELECT ROW(p.entity_id, p.firstname, p.lastname, p.initials)::persontype FROM
onp_crm_person pWHERE act.responsible = p.onp_user_id) AS responsible_person
fromonp_crm_activity act ) AS q ORDER BY (q.responsible_person).initials ASC ;
 
Without the outer query:
SELECT act.entity_id AS project_id, act.name AS project , (SELECT ROW
(p.entity_id, p.firstname, p.lastname, p.initials)::persontypeFROM
onp_crm_person pWHERE act.responsible = p.onp_user_id) AS responsible_person
fromonp_crm_activity act ORDER BY (responsible_person).initials ; It fails with:
ERROR:  column "responsible_person" does not exist
LINE 6: ORDER BY (responsible_person).initials
 

 
Is the only solution wrapping with an outer query?
 
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com>
www.visena.com <https://www.visena.com>
<https://www.visena.com>

 

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2018-01-11 15:40:30 Updating a pre-10 partitioned table to use PG 10 partitioning
Previous Message Durumdara 2018-01-11 12:23:47 Re: String comparison problem in select - too many results