Editor

Monthly revenue and the change from the month before

Hard · Online shop database · Window functions, WITH (CTE), Date functions

Work out the revenue of each month from delivered orders (quantity times unit_price, summed). Months without delivered orders are not listed, so each month is compared with the previous month in the list.

Return the month as YYYY-MM, its revenue, and the change (this month's revenue minus the previous listed month's), both rounded to 2 decimals with round, in that order, sorted by month. The first month's change is NULL.

Expected result

monthrevenuechange
2025-02255.94NULL
2025-0389.5-166.44
2025-04152.9963.49
2025-05180.9928
2025-0682.43-98.56
2025-0711936.57
2025-0896.88-22.12
2025-10112.4915.61
2025-1139-73.49
2025-12155.48116.48
2026-0123579.52
2026-0258.47-176.53
2026-03146.588.03
The online shop tables

customers: 18 rows, first 5 shown

idnamecitycountrysignup_datephone
1Ravi KumarBengaluruIndia2025-01-12+91 98450 12345
2Emily JohnsonAustinUSA2025-01-25+1 512 555 0142
3Hiroshi SatoOsakaJapan2025-02-08NULL
4Ana SouzaSao PauloBrazil2025-02-19+55 11 91234 5678
5Priya SharmaMumbaiIndia2025-03-03+91 99200 54321

order_items: 40 rows, first 5 shown

idorder_idproduct_idquantityunit_price
111122.99
21933.99
3241189
4216215.99
532189.5

orders: 28 rows, first 5 shown

idcustomer_idorder_datestatus
112025-02-03delivered
222025-02-14delivered
332025-03-01delivered
412025-03-22cancelled
542025-04-05delivered

products: 18 rows, first 5 shown

idnamecategorypricestock
1Wireless MouseElectronics24.99120
2Mechanical KeyboardElectronics89.545
3USB-C HubElectronics3980
4Noise Cancelling HeadphonesElectronics19925
5Intro to StatisticsBooks4860
-- Write your SQL here