Oracle 按最后一个 NULL 降序排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17639837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:47:09  来源:igfitidea点击:

Oracle order by descending with NULL last

sqloracle

提问by sasori

my objective is, to print the result of the query in "DESCENDING" order. but the problem is, the rows with NULL values went on top of the list.. how to put the null rows at the bottom, if the order by is descending?

我的目标是,以“降序”的顺序打印查询结果。但问题是,具有 NULL 值的行在列表的顶部。如果 order by 是降序,如何将空行放在底部?

select mysubcat.subcat
       , mysubcat.subcatid as subcat_id
       , (select SUM(myad.PAGEVIEW) 
           from myad 
            where MYAD.CREATEDDATE between  '01-JUL-13 02.00.49.000000000 PM' and '13-JUL-13 02.00.49.000000000 PM'
            AND MYAD.status = 1 
            and  MYAD.mobileapp IS NULL
            and myad.subcatid = mysubcat.subcatid )as web_views 
from mysubcat 
order by web_views desc;

the sample result goes like this

样本结果是这样的

                             SUBCAT_ID    WEB_VIEWS
Swimming Lessons                56        (null)    
Medical Services                17        (null)
Mobile Phones & Tablets         39        6519
Home Furnishing & Renovation   109        4519

the order is in the descending order, I just want to put the rows with null values at the bottom of the printed result, so how?

顺序是降序,我只想把空值的行放在打印结果的底部,那怎么办?

回答by NINCOMPOOP

You can use DESC NULLS LASTto achieve that.

你可以用它DESC NULLS LAST来实现。

Here is the official documentationfrom Oracle.

这是Oracle的官方文档

NULLS LAST

Specifies that NULL values should be returned after non-NULL values.

最后一个空

指定应在非 NULL 值之后返回 NULL 值。

回答by juergen d

Use a case

用一个 case

order by case when web_views is not null 
              then 1 
              else 2 
         end asc, 
         web_views desc;