i want to query one table to find the sum of charges for a good within the months of Jan - May of 2018 - 2019. i know how to conditionally sum to get the charges broken out by year, but i'm unsure how to also build out a subquery to calculate the month over month change in sums between the years.
here's what my query looks like now:
SELECT
"Month Bought" AS MONTH,
SUM(
CASE
WHEN "Bought Year" = '2018'
THEN "total charges"
ELSE 0
END) AS Charge_2018,
SUM(
CASE
WHEN "Bought Year" = '2019'
THEN "total charges"
ELSE 0
END) AS Charge_2019
FROM
Sales_agg
WHERE
"Month Bought" IS NOT NULL
AND "Month Bought" between '1' and '5'
GROUP BY
"Month Bought"
ORDER BY
"Month Bought";
here's what the output looks like now:
Month | Charge_2018 | Charge_ 2019
1 5 7
2 7 8
3 7 10
4 6 8
5 5 6
here's what i'd like the output to look like:
Month | Charge_2018 | Charge_ 2019 | Mom_Change
1 5 7 0.XX
2 7 8 0.XX
3 7 10 0.XX
4 6 8 0.XX
5 5 6 0.XX
thanks so much for any help!