pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Is ORDER BY in sub-query preserved when outer query is only projection?
4+ messages / 4 participants
[nested] [flat]

* Is ORDER BY in sub-query preserved when outer query is only projection?
@ 2018-01-14 12:03  Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Andreas Joseph Krogh @ 2018-01-14 12:03 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi.
 
I have this query:
 
SELECT q.* FROM (
  SELECT comp.id, comp.name
  FROM company comp JOIN req r ON r.company_id = comp.id
  ORDER BY LOWER(comp.name) ASC
) AS q
ORDER BY r.status ASC
 
What I'm trying to do here is to order by some status (which may be only 1 of 
3 values, for instance OPEN, IN_PROGRESS, CLOSED), then order by company-name 
so I get results for each status sorted by company-name.
 
Is this kind of sort stable, can I assume the pre-sorted result's order is 
preserved so I achieve what I want?
 
Thanks.
 
-- 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;

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

* Re: Is ORDER BY in sub-query preserved when outer query is only projection?
@ 2018-01-14 12:30  Francisco Olarte <folarte@peoplecall.com>
  parent: Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Francisco Olarte @ 2018-01-14 12:30 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-general@lists.postgresql.org

Andreas:

On Sun, Jan 14, 2018 at 1:03 PM, Andreas Joseph Krogh
<andreas@visena.com> wrote:
> SELECT q.* FROM (
>   SELECT comp.id, comp.name
>   FROM company comp JOIN req r ON r.company_id = comp.id
>   ORDER BY LOWER(comp.name) ASC
> ) AS q
> ORDER BY r.status ASC
>
> What I'm trying to do here is to order by some status (which may be only 1 of 3 values, for instance OPEN, IN_PROGRESS, CLOSED), then order by company-name so I get results for each status sorted by company-name.
>
> Is this kind of sort stable, can I assume the pre-sorted result's order is preserved so I achieve what I want?

I doubt it is mandated to be stable. But IIRC you can sort by a
non-returned field, so you should be able to do it in just one query (
http://sqlfiddle.com/#!17/aaa62/3 )

I would try

 SELECT comp.id, comp.name
 FROM company comp JOIN req r ON r.company_id = comp.id
 ORDER BY  r.status ASC, LOWER(comp.name) ASC

Francisco Olarte.




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

* Re: Re: Is ORDER BY in sub-query preserved when outer query is only projection?
@ 2018-01-15 17:48  Haroldo Stenger <haroldo.stenger@gmail.com>
  parent: Francisco Olarte <folarte@peoplecall.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Haroldo Stenger @ 2018-01-15 17:48 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-general@lists.postgresql.org

hi, maybe selecting the relevant fields to order-by alongside (next to) the
row()'ed part, they will be selected  "twice", but you'll be able to
reference them.

regards


Haroldo

2018-01-14 9:40 GMT-03:00 Andreas Joseph Krogh <andreas@visena.com>:

> På søndag 14. januar 2018 kl. 13:30:29, skrev Francisco Olarte <
> folarte@peoplecall.com>:
>
> Andreas:
>
> On Sun, Jan 14, 2018 at 1:03 PM, Andreas Joseph Krogh
> <andreas@visena.com> wrote:
> > SELECT q.* FROM (
> >   SELECT comp.id, comp.name
> >   FROM company comp JOIN req r ON r.company_id = comp.id
> >   ORDER BY LOWER(comp.name) ASC
> > ) AS q
> > ORDER BY r.status ASC
> >
> > What I'm trying to do here is to order by some status (which may be only
> 1 of 3 values, for instance OPEN, IN_PROGRESS, CLOSED), then order by
> company-name so I get results for each status sorted by company-name.
> >
> > Is this kind of sort stable, can I assume the pre-sorted result's order
> is preserved so I achieve what I want?
>
> I doubt it is mandated to be stable. But IIRC you can sort by a
> non-returned field, so you should be able to do it in just one query (
> http://sqlfiddle.com/#!17/aaa62/3 )
>
> I would try
>
>  SELECT comp.id, comp.name
>  FROM company comp JOIN req r ON r.company_id = comp.id
>  ORDER BY  r.status ASC, LOWER(comp.name) ASC
>
> Francisco Olarte.
>
>
> Thanks, but my real query is more complex and I need to sort on a custom
> coposite type so I think I *need* an outer query for that, see my post
> here: http://www.postgresql-archive.org/ORDER-BY-custom-
> type-td6000437.html
>
> Do you see any solution sorting on a composite type without using an outer
> query?
>
> --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas@visena.com
> www.visena.com
> <https://www.visena.com;
>
>

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

* Re: Re: Is ORDER BY in sub-query preserved when outer query is only projection?
@ 2018-01-15 18:04  David G. Johnston <david.g.johnston@gmail.com>
  parent: Francisco Olarte <folarte@peoplecall.com>
  1 sibling, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2018-01-15 18:04 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Sun, Jan 14, 2018 at 5:40 AM, Andreas Joseph Krogh <andreas@visena.com>
wrote:

> På søndag 14. januar 2018 kl. 13:30:29, skrev Francisco Olarte <
> folarte@peoplecall.com>:
>
> Andreas:
>
> On Sun, Jan 14, 2018 at 1:03 PM, Andreas Joseph Krogh
> <andreas@visena.com> wrote:
> > SELECT q.* FROM (
> >   SELECT comp.id, comp.name
> >   FROM company comp JOIN req r ON r.company_id = comp.id
> >   ORDER BY LOWER(comp.name) ASC
> > ) AS q
> > ORDER BY r.status ASC
>
>
> Do you see any solution sorting on a composite type without using an outer
> query?
>
>
​Tacking on ORDER BY to an inner query is generally not the right thing to
do.

​What can you not write:

SELECT q.* FROM () AS q
ORDER BY lower(q.name)​ ASC, q.status ASC
?

Also, ORDER BY r.status ASC in you original query shouldn't work - r is not
visible at that point, only q is.

David J.

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


end of thread, other threads:[~2018-01-15 18:04 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-14 12:03 Is ORDER BY in sub-query preserved when outer query is only projection? Andreas Joseph Krogh <andreas@visena.com>
2018-01-14 12:30 ` Francisco Olarte <folarte@peoplecall.com>
2018-01-15 17:48   ` Haroldo Stenger <haroldo.stenger@gmail.com>
2018-01-15 18:04   ` David G. Johnston <david.g.johnston@gmail.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