Oracle最后以NULL递减降序

时间:2013-07-14 13:30:43

标签: sql oracle

我的目标是,以“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;

样本结果如下

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

顺序是降序,我只是想在打印结果的底部放置带有空值的行,那么如何?

2 个答案:

答案 0 :(得分:28)

您可以使用DESC NULLS LAST来实现这一目标。

以下是Oracle的official documentation

  

NULLS LAST

     

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

答案 1 :(得分:7)

使用case

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