How can i calculate and show the percentage of growth or loss, on the previous month (comparing the figure from the previous month, not previous year).
i.e. Jan is 500, so results will be 0%. Then as Feb is 150, the percentage loss is -70%, March will show -86.67 as their results were only 20 (compared to Febs 150)
+----------+------+------------+
| Month | Sale | Difference |
+----------+------+------------|
| January | 500 | |
| February | 150 | -70.00 |
| March | 20 | -86.67 |
| April | 250 | 1150.00 |
| May | 280 | 12.00 |
| June | 400 | 42.86 |
| July | 480 | 20.00 |
+----------+------+------------+
My script below produces: (I just need to add another percentage column
+----------+-------------------+
| MONTH | SUM(SALES_AMOUNT) |
+----------+-------------------+
| January | 500 |
| February | 150 |
| March | 20 |
| April | 250 |
| May | 280 |
| June | 400 |
| July | 480 |
+----------+-------------------+
SELECT coalesce(date_format(DATE_PURCHASED, '%M')) AS MONTH,
SUM(SALE_PRICE)
FROM SALE
WHERE YEAR(DATE_PURCHASED) = 2017
GROUP BY month
You might left join the query to itself on the number of the month minus 1. Then you had the sum of the month an the last month in one row and could calculate the percentage. Similar to the following:
Note
A
andB
being your query, just modified so that the month is numeric as this is needed in theON
clause.