SQL: Exception to an otherwise sorted result set

冷暖自知 提交于 2019-12-12 02:34:51

问题


Using Oracle 11g

I want a single exception to an otherwise sorted table

select fruit as popluar_choices  
from menu 
order by fruit /* Exception put 'grapefruit' at top of list */

Desired Result

popular_choices
-----------
grapefruit
apple
fig
kiwi
lemon
pear

It's similar to this post: How to apply non standard SQL column sort order?


回答1:


  select fruit as popluar_choices  
    from menu 
order by case fruit when 'grapefruit' then 0
                                      else 1
              end,
         fruit



回答2:


SELECT fruit AS popular_choices FROM menu
ORDER BY 
    CASE fruit
        WHEN 'grapefruit' THEN ''
        ELSE fruit
    END



回答3:


select fruit as popluar_choices  
from menu 
order by CASE fruit = 'grapefruit' THEN '__' ELSE fruit END


来源:https://stackoverflow.com/questions/9284536/sql-exception-to-an-otherwise-sorted-result-set

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