pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreas@visena.com>
To: pgsql-general@lists.postgresql.org
Subject: Sv: ORDER BY custom type
Date: Thu, 11 Jan 2018 13:51:21 +0100 (CET)
Message-ID: <VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena> (raw)
In-Reply-To: <VisenaEmail.167.abde9aaf9b1c0c53.160e4d866b9@tc7-visena>

På torsdag 11. januar 2018 kl. 12:00:55, skrev Andreas Joseph Krogh <
andreas@visena.com <mailto:andreas@visena.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@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
 <https://www.visena.com;


 

Message-ID: <VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena>
Permalink:  ../VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena/
Also on:    postgresql.org/message-id/VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andreas@visena.com
  Subject: Re: ORDER BY custom type
  In-Reply-To: <VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena>
  References: <VisenaEmail.167.abde9aaf9b1c0c53.160e4d866b9@tc7-visena> <VisenaEmail.16e.6f0ceedf3cd46f94.160e543e392@tc7-visena>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox