Sql Order By - Why Is It Not Working Here?
This is the table I have: +---------------------+--------+----------+ | date | sku | quantity | +---------------------+--------+----------+ | 2017-08-23 14:58:00
Solution 1:
You are ordering by quantity. Because it is not aggregated, you are getting an arbitrary value from each group. You need to order by the total. One method is:
order by sum(quantity) desc
However, I would recommend assigning a reasonable alias (one that doesn't need to be escaped) and using that:
select sku,sum(quantity) as total_quantity
from transactions
whereDATE(date) between'2017-07-23'and'2017-09-23'groupby sku
orderby total_quantity desc;
Solution 2:
You need to order it by the SUM(quantity).
SELECT sku, SUM(quantity) AS'total quant'FROM transactions
WHEREDATE(date) BETWEEN'2017-07-23'AND'2017-09-23'GROUPBY sku
ORDERBYSUM(quantity) DESC;
Solution 3:
You should use:
order by sum(quantity)
Post a Comment for "Sql Order By - Why Is It Not Working Here?"