0Day Forums
Oracle order by descending with NULL last - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Database (https://0day.red/Forum-Database)
+---- Forum: Oracle (https://0day.red/Forum-Oracle)
+---- Thread: Oracle order by descending with NULL last (/Thread-Oracle-order-by-descending-with-NULL-last)



Oracle order by descending with NULL last - madox17 - 07-31-2023

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?

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?


RE: Oracle order by descending with NULL last - Otternqqycp - 07-31-2023

Use a `case`

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


RE: Oracle order by descending with NULL last - suzied - 07-31-2023

You can use `DESC NULLS LAST` to achieve that.

Here is the [official documentation][1] from Oracle.

>**NULLS LAST**
>
>Specifies that NULL values should be returned after non-NULL values.

[1]:http://docs.oracle.com/javadb/10.8.1.2/ref/rrefsqlj13658.html