-2
SELECT item_description, item_variant, branch, GROUP_CONCAT(sum ORDER BY 'date') AS chartData
        FROM (
        SELECT item_description, item_variant, branch, SUM(sales) AS sum
        FROM inventory_branches
        WHERE (item_description = 'agapanthus') AND (date BETWEEN '2018-06' AND '2018-08')
        GROUP BY item_description, item_variant, branch, MONTH(date) DESC
        ) T
        GROUP BY item_description, item_variant, branch, MONTH('date') 
        LIMIT 5

The code above returns all rows correctly except for the first one. The first group of data in chartData is in reverse

In the first row, the 20 should be after the 58

Thank you in advance!

lekendary
  • 3
  • 3
  • Do you mean `GROUP_CONCAT(sum ORDER BY 'date' DESC) AS chartData`? – Zack Oct 08 '18 at 16:56
  • 2
    See: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Oct 08 '18 at 16:56
  • @Zack no matter how I add DESC or ASC in the code, the result in the first row of `chartData` is reversed – lekendary Oct 08 '18 at 17:11

1 Answers1

2

I assume there is a column named date in you table. To sort by it use ORDER BY date and not ORDER BY 'date', i.e. don't enclose the column name in single quotes. 'date' is just a string literal, sorting by it is like not sorting at all, as it is equal for all rows.

sticky bit
  • 36,626
  • 12
  • 31
  • 42