pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Order by lower(column-alias) doesn't work...
5+ messages / 3 participants
[nested] [flat]

* Order by lower(column-alias) doesn't work...
@ 2020-05-28 12:14  Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Andreas Joseph Krogh @ 2020-05-28 12:14 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


Hi. 

This works: 
select p.firstname, p.lastname, p.firstname || p.lastname as fullname from 
onp_crm_person p order by fullname;

But this doesn't: 
select p.firstname, p.lastname, p.firstname || p.lastname as fullname from 
onp_crm_person p order by lower(fullname);
ERROR: column "fullname" does not exist 
 LINE 1: ... as fullname from onp_crm_person p order by lower(fullname);

This is just an example-query, in my real query I have a more complex query 
generating an array of a custom-type which is then referenced to as a 
column-alias, and then ORDER BY on a function tranforming this array doesn't 
work: 

SELECT ... 
ARRAY(WITH RECURSIVE t AS (SELECT 
...
) SELECT ROW(t.entity_id, t.name)::BigIntVarChar from t order by level DESC) 
as folder_parent_array


ORDER BY bigintvarchar_to_text_value_flatten(folder_parent_array) ASC; 
column "folder_parent_array" does not exist 

What bigintvarchar_to_text_value_flatten() does is to take the "varchar"-part 
out of the BigintVarchar-type and "flatten" the array by that value so that it 
sorts nicely. 

 Any way round this? 


--
 Andreas Joseph Krogh 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Order by lower(column-alias) doesn't work...
@ 2020-05-28 12:50  Geoff Winkless <pgsqladmin@geoff.dj>
  parent: Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Geoff Winkless @ 2020-05-28 12:50 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, 28 May 2020 at 13:14, Andreas Joseph Krogh <andreas@visena.com> wrote:
> This works:
> select p.firstname, p.lastname, p.firstname || p.lastname as fullname from onp_crm_person p order by fullname;
>
> But this doesn't:
> select p.firstname, p.lastname, p.firstname || p.lastname as fullname from onp_crm_person p order by lower(fullname);
> ERROR:  column "fullname" does not exist
> LINE 1: ... as fullname from onp_crm_person p order by lower(fullname);

Wrap the original query in either a CTE or a temporary table.
eg

=> SELECT REPLACE(name, '_', ' ') AS nm FROM subs ORDER BY lower(nm);
ERROR:  column "nm" does not exist
=> SELECT * FROM (SELECT REPLACE(name, '_', ' ') AS nm FROM subs) AS t
ORDER BY lower(nm);
[results]
=> WITH t AS (SELECT REPLACE(name, '_', ' ') AS nm FROM subs)   SELECT
* FROM t ORDER BY lower(nm);
[results]

Geoff





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Order by lower(column-alias) doesn't work...
@ 2020-05-28 12:55  Andreas Joseph Krogh <andreas@visena.com>
  parent: Geoff Winkless <pgsqladmin@geoff.dj>
  0 siblings, 1 reply; 5+ messages in thread

From: Andreas Joseph Krogh @ 2020-05-28 12:55 UTC (permalink / raw)
  To: Geoff Winkless <pgsqladmin@geoff.dj>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>


På torsdag 28. mai 2020 kl. 14:50:54, skrev Geoff Winkless <pgsqladmin@geoff.dj
 <mailto:pgsqladmin@geoff.dj>>: 
On Thu, 28 May 2020 at 13:14, Andreas Joseph Krogh <andreas@visena.com> wrote:
 > This works:
 > select p.firstname, p.lastname, p.firstname || p.lastname as fullname from 
onp_crm_person p order by fullname;
 >
 > But this doesn't:
 > select p.firstname, p.lastname, p.firstname || p.lastname as fullname from 
onp_crm_person p order by lower(fullname);
 > ERROR: column "fullname" does not exist
 > LINE 1: ... as fullname from onp_crm_person p order by lower(fullname);

 Wrap the original query in either a CTE or a temporary table.
 eg
 [..] 

Yea, I was hoping to avoid that, as the query is generated and rewriting it is 
a pain... 

Is there a way to define "sorting-rules" on custom-types so that I can have 
ORDER BY <my_array_of_custom_types_alias> and PG will pick my custom odering? 


--
 Andreas Joseph Krogh

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Order by lower(column-alias) doesn't work...
@ 2020-05-28 13:26  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2020-05-28 13:26 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: Geoff Winkless <pgsqladmin@geoff.dj>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Andreas Joseph Krogh <andreas@visena.com> writes:
> Is there a way to define "sorting-rules" on custom-types so that I can have 
> ORDER BY <my_array_of_custom_types_alias> and PG will pick my custom odering? 

You'd have to write your own type, which would be a lotta work :-(.

A possible partial answer is to define the composite type as

	firstname citext, lastname citext, other-fields-here

and then the regular composite-type comparison rule would give you
approximately what you said you wanted ... but only approximately.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Order by lower(column-alias) doesn't work...
@ 2020-05-28 13:36  Andreas Joseph Krogh <andreas@visena.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Andreas Joseph Krogh @ 2020-05-28 13:36 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Geoff Winkless <pgsqladmin@geoff.dj>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>


På torsdag 28. mai 2020 kl. 15:26:42, skrev Tom Lane <tgl@sss.pgh.pa.us 
<mailto:tgl@sss.pgh.pa.us>>: 
Andreas Joseph Krogh <andreas@visena.com> writes:
 > Is there a way to define "sorting-rules" on custom-types so that I can have
 > ORDER BY <my_array_of_custom_types_alias> and PG will pick my custom 
odering?

 You'd have to write your own type, which would be a lotta work :-(.

 A possible partial answer is to define the composite type as

 firstname citext, lastname citext, other-fields-here

 and then the regular composite-type comparison rule would give you
 approximately what you said you wanted ... but only approximately.

 regards, tom lane 


Hm, ok. I think the most non-intrusive way for me is to craft a sub-select 
producing the "varchar-string-flattened" so I can order by that alias. 


--
 Andreas Joseph Krogh

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2020-05-28 13:36 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-05-28 12:14 Order by lower(column-alias) doesn't work... Andreas Joseph Krogh <andreas@visena.com>
2020-05-28 12:50 ` Geoff Winkless <pgsqladmin@geoff.dj>
2020-05-28 12:55   ` Andreas Joseph Krogh <andreas@visena.com>
2020-05-28 13:26     ` Tom Lane <tgl@sss.pgh.pa.us>
2020-05-28 13:36       ` Andreas Joseph Krogh <andreas@visena.com>

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