14

我的目标是,以“降序”顺序打印查询结果。但问题是,具有 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

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

4

2 回答 2

29

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

这是 Oracle 的官方文档

NULLS LAST

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

于 2013-07-14T13:33:03.000 回答
7

用一个 case

order by case when web_views is not null 
              then 1 
              else 2 
         end asc, 
         web_views desc;
于 2013-07-14T13:32:28.983 回答