razorcode7
razorcode7

Reputation: 55

Descending order in sql

Can't seem to get my column to show in Descending order.

SELECT ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM Employee),2)||" %" AS "% of Employee", R.Region AS Region
FROM Employee R
GROUP BY R.Region
ORDER DESC "% of Employee" DESC

It prints out the order of Region in DESC instead of % of employee

% of Employee | Region
----------------------- 
3.08%         | Darwin
20.64%        | Copley
2.44%         | Brisbane
2.44%         | Albany

Upvotes: 0

Views: 282

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270001

The DESC goes at the end of the ORDER BY:

SELECT ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM Employee),2)||" %" AS "% of Employee",
       R.Region AS Region
FROM Employee R
GROUP BY R.Region
ORDER BY "% of Employee" DESC

Upvotes: 1

Felix Pamittan
Felix Pamittan

Reputation: 31879

That's because you're ordering on VARCHAR. The Regions being displayed in descending order is just a coincidence. You need to order by just the COUNT(*) instead:

SELECT 
    ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM Employee),2)||" %" AS "% of Employee", R.Region 
    AS Region
FROM Employee R
GROUP BY R.Region
ORDER BY COUNT(*) DESC

Upvotes: 1

Related Questions