sql >> Base de Datos >  >> RDS >> PostgreSQL

¿Cómo usar un ALIAS en una cláusula ORDER BY de PostgreSQL?

Siempre puedes ORDER BY de esta manera:

select 
    title, 
    ( stock_one + stock_two ) as global_stock
from product
order by 2, 1

o envuélvelo en otro SELECT:

SELECT *
from
(
    select 
        title, 
        ( stock_one + stock_two ) as global_stock
    from product
) x
order by (case when global_stock = 0 then 1 else 0 end) desc, title