This is the table I have:
+---------------------+--------+----------+
| date | sku | quantity |
+---------------------+--------+----------+
| 2017-08-23 14:58:00 | 123333 | 2 |
| 2017-08-23 14:58:00 | 123333 | 1 |
| 2017-08-23 14:58:00 | 123333 | 1 |
| 2017-08-23 14:58:00 | 123337 | 2 |
| 2017-08-23 14:58:00 | 123335 | 1 |
| 2017-08-23 14:58:00 | 123331 | 1 |
| 2017-08-23 14:58:00 | 123332 | 2 |
| 2017-08-23 14:58:00 | 123333 | 1 |
| 2017-08-23 14:58:00 | 123334 | 1 |
| 2017-08-23 14:58:00 | 123334 | 3 |
+---------------------+--------+----------+
I would like to perform an select statement as follows:
select sku,sum(quantity) as 'total quant' from transactions
where DATE(date) between '2017-07-23' and '2017-09-23'
group by sku
order by quantity desc;
But I am getting this:
+--------+-------------+
| sku | total quant |
+--------+-------------+
| 123332 | 2 |
| 123337 | 2 |
| 123333 | 5 |
| 123334 | 4 |
| 123335 | 1 |
| 123331 | 1 |
+--------+-------------+
Which is NOT in the order as I am expecting. Why is order by not working here?
SIDE NOTE My quantity
column is of data type int
and I have tried casting as suggested in this post but to no avail.