Re: Is ORDER BY in sub-query preserved when outer query is only projection?

Поиск
Список
Период
Сортировка
От Francisco Olarte
Тема Re: Is ORDER BY in sub-query preserved when outer query is only projection?
Дата
Msg-id CA+bJJbzssyBPY=8b_wUQwt3wNOoBQcJtJT-sdaV+08VNgUNwYA@mail.gmail.com
обсуждение исходный текст
Ответ на Is ORDER BY in sub-query preserved when outer query is only projection?  (Andreas Joseph Krogh <andreas@visena.com>)
Ответы Sv: Re: Is ORDER BY in sub-query preserved when outer query is only projection?  (Andreas Joseph Krogh <andreas@visena.com>)
Список pgsql-general
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.


В списке pgsql-general по дате отправления:

Предыдущее
От: Francisco Olarte
Дата:
Сообщение: Re: String comparison problem in select - too many results
Следующее
От: Andreas Joseph Krogh
Дата:
Сообщение: Sv: Re: Is ORDER BY in sub-query preserved when outer query is only projection?