For each month of 2017, calculate the total monthly revenue from orders shipped to the USA and the percentage revenue change (delta) as compared to the previous month. Show three columns: revenue_month
, total_revenue
, and delta_percentage
.
In the first row, leave the delta value as NULL
. Order the rows by month. Here's how you can count delta_percentage
:
\frac{\textrm{total amount} - \textrm{total amount from preceding months}}{\textrm{total amount from preceding months}} \cdot 100
Remember to round the result to the second decimal point.
Use the following expression in the last column:
ROUND(
100 *
(SUM(amount) - LAG(SUM(amount), 1) OVER (ORDER BY EXTRACT(month FROM order_date)))
/ CAST(LAG(SUM(amount), 1) OVER (ORDER BY EXTRACT(month FROM order_date)) AS decimal(10, 2)),
2) AS delta_percentage