I am working on the orders table provided by this site, it has its own editor where you can test your SQL statements.
The order table looks like this
order_id | customer_id | order_date |
---|---|---|
1 | 7000 | 2016/04/18 |
2 | 5000 | 2016/04/18 |
3 | 8000 | 2016/04/19 |
4 | 4000 | 2016/04/20 |
5 | NULL | 2016/05/01 |
I want to get the difference in the number of orders for subsequent months.
To elaborate, the number of orders each month would be like this
SQL Statement
SELECT
MONTH(order_date) AS Month,
COUNT(MONTH(order_date)) AS Total_Orders
FROM
orders
GROUP BY
MONTH(order_date)
Result:
Month | Total_Orders |
---|---|
4 | 4 |
5 | 1 |
Now my goal is to get the difference in subsequent months which would be
Month | Total_Orders | Total_Orders_Diff |
---|---|---|
4 | 4 | 4 - Null = Null |
5 | 1 | 1 - 4 = -3 |
My strategy was to self-join following this answer
This was my attempt
SELECT
MONTH(a.order_date),
COUNT(MONTH(a.order_date)),
COUNT(MONTH(b.order_date)) - COUNT(MONTH(a.order_date)) AS prev,
MONTH(b.order_date)
FROM
orders a
LEFT JOIN
orders b ON MONTH(a.order_date) = MONTH(b.order_date) - 1
GROUP BY
MONTH(a.order_date)
However, the result was just zeros (as shown below) which suggests that I am just subtracting from the same value rather than from the previous month (or subtracting from a null value)
MONTH(a.order_date) | COUNT(MONTH(a.order_date)) | prev | MONTH(b.order_date) |
---|---|---|---|
4 | 4 | 0 | NULL |
5 | 1 | 0 | NULL |
Do you have any suggestions as to what I am doing wrong?