"ORDER BY" Command Assistance in SQLDeveloper
Hi guys I am trying to display the name,开发者_运维知识库 salary and commission for all employees who earn commissions and sorting the data in descending order of salary and commissions.
I have written the following code but I dont think its right. Does anyone know if I have sorted it in descending order of salary AND commissions?
SELECT ENAME, SAL, COMM
FROM emp
WHERE COMM IS NOT null
ORDER BY SAL,COMM DESC;
Thanks a lot guys.
-Jay
No, you need to do ORDER BY SAL DESC,COMM DESC;
The DESC
only applies to the immediately preceding expression. Not all of the preceding expressions, which means in your original query SAL
will still be sorted ascending (the default when no sort order is explicitly specified).
try this
ORDER BY SAL DESC, COMM DESC
select * from employees order by salary desc,commission desc
EDITED
you will be more clear with @Martin Smith answer i checked just now.
You may also use the ROW_NUMBER() feature.
Check this out: http://www.sqlservercentral.com/articles/T-SQL/66512/
This is one step ahead for such requirements.
Hope this helps.
精彩评论