ORDER BY Alias not working

时间秒杀一切 提交于 2019-11-29 07:46:38

You can use ORDER BY 1 to order by the first field, which is "Fruits". The same is valid for GROUP BY

Update

For the order, instead of doing the case in the order by, create a new column in.. say.. the second position:

(CASE 
    WHEN "Fruits" = 'Apple' THEN 1 
    WHEN "Fruits" = 'Pear' THEN 2 
    WHEN "Fruits" = 'Grapes' THEN 3 
    ELSE 4 ) as Order

Then in you ORDER BY 2.

The reason for this can be found in the documentation:

Each expression [in the ORDER BY list] can be the name or ordinal number of an output column (SELECT list item), or it can be an arbitrary expression formed from input-column values.

(my emphasis)

The reason for this is that old versions of the SQL standard (SQL-92) only allowed sorting by output column name or number, whereas newer versions allow sorting by arbitrary expressions, but those expressions are formed from input column values.

Other answers already contain various suitable workarounds for your case.

The alias is assigned after the order by so you can't use it in the order by. Use this instead:

(CASE
    WHEN tof."TypeOfFruits" = 'A' THEN 'Apple' 
    WHEN tof."TypeOfFruits" = 'P' THEN 'Pear' 
    WHEN tof."TypeOfFruits" = 'G' THEN 'Grapes' 
    ELSE 'Other' END)

Consider something like this:

SELECT * FROM (SELECT (CASE
    WHEN tof."TypeOfFruits" = 'A' THEN 'Apple' 
    WHEN tof."TypeOfFruits" = 'P' THEN 'Pear' 
    WHEN tof."TypeOfFruits" = 'G' THEN 'Grapes' 
    ELSE 'Other' END) AS "Fruits", 
    (CASE 
    WHEN tof."TypeOfFruits" = 'A' THEN 1 
    WHEN tof."TypeOfFruits" = 'P' THEN 2 
    WHEN tof."TypeOfFruits" = 'G' THEN 3 
    ELSE 4 END) as NUM

        FROM ..... <rest of your query without group by and order by .....
    )

GROUP BY Fruits
ORDER BY NUM

You could try something like this ... untested, but I've seen similar queries.
Let me know if it works...

SELECT "Fruits",
    SUM(CASE WHEN r.order_date 
        BETWEEN DATE_TRUNC('DAY', LOCALTIMESTAMP) AND DATE_TRUNC('DAY', LOCALTIMESTAMP) + INTERVAL '1 DAY' 
        THEN 1 ELSE 0 END) AS daily, 
    SUM(CASE WHEN r.order_date 
        BETWEEN DATE_TRUNC('MONTH', LOCALTIMESTAMP) AND DATE_TRUNC('MONTH', LOCALTIMESTAMP) + INTERVAL '1 MONTH' 
        THEN 1 ELSE 0 END) AS monthly, 
    SUM(CASE WHEN r.order_date 
        BETWEEN DATE_TRUNC('YEAR', LOCALTIMESTAMP) AND DATE_TRUNC('YEAR', LOCALTIMESTAMP) + INTERVAL '1 YEAR' 
        THEN 1 ELSE 0 END) AS yearly, 
    SUM(CASE WHEN r.order_date >= '01-01-2011 00:00:00' THEN 1 ELSE 0 END) AS lifetime 
FROM reports AS r
    ,(SELECT "ID",
             CASE
                WHEN tof."TypeOfFruits" = 'A' THEN 'Apple' 
                WHEN tof."TypeOfFruits" = 'P' THEN 'Pear' 
                WHEN tof."TypeOfFruits" = 'G' THEN 'Grapes' 
                ELSE 'Other'
             END AS "Fruits" FROM "TypeOfFruits" ) AS "tof"
WHERE r.id = tof."ID" 
GROUP BY "Fruits"
ORDER BY CASE 
    WHEN "Fruits" = 'Apple' THEN 1 
    WHEN "Fruits" = 'Pear' THEN 2 
    WHEN "Fruits" = 'Grapes' THEN 3 
    ELSE 4 
END

Try using backticks (`) instead of single/double quotes to wrap your alias name.

Had the same issue with MySQL; backticks fixed the problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!