'can I know what I can use in my ORDER BY statement for the sum be from highest to lowest?

I'm pretty stuck in what to do. I tried everything but it still won't sort from the highest total first.

my query: (yes i tried order by 2 but it didn't work. I'm still new at this so i might look dumb at this lol)

SELECT c.cust_no, c.cname, c.city, o.order_no, SUM(ol.price*ol.qty) 
FROM customers c 
JOIN orders o ON c.cust_no = o.cust_no 
JOIN orderlines ol ON o.order_no = ol.order_no 
WHERE c.city LIKE 'S%'
GROUP BY c.cust_no, c.cname, c.city, o. order_no 
ORDER BY 2 ;
sql


Solution 1:[1]

I think you want ORDER BY 5 DESC:

SELECT c.cust_no, c.cname, c.city, o.order_no, SUM(ol.price*ol.qty)
FROM customers c
INNER JOIN orders o ON c.cust_no = o.cust_no
INNER JOIN orderlines ol ON o.order_no = ol.order_no
WHERE c.city LIKE 'S%'
GROUP BY c.cust_no, c.cname, c.city, o. order_no
ORDER BY 5 DESC;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Biegeleisen