Back to questions
This is the same question as problem #32 in the SQL Chapter of Ace the Data Science Interview!
Assume you are given the table below containing information on user transactions for particular products. Write a query to obtain the year-on-year growth rate for the total spend of each product for each year.
Output the year (in ascending order) partitioned by product id, current year's spend, previous year's spend and year-on-year growth rate (percentage rounded to 2 decimal places).
The third row in the example output shows that the spend for product 123424 grew 24.62% from 1000.20 in 2020 to 1246.44 in 2021.
The dataset you are querying against may have different input & output - this is just an example!
Our goal for this question is to find the year-on-year (y-o-y) growth rate for Wayfair's user spend.
Our multi-step approach for solving the question:
First, we need to obtain the year by using on the transaction date as written in the code below.
In this output table, you can see that the spend for product id 234412 is summarized by year. Take note that you would have to query your output for all the product ids.
Next, we convert the query in step 1 into a CTE called (you can name the CTE as you wish). With this CTE, we then calculate the prior year’s spend for each product. We can do so by applying function onto each year and partitioning by product id to calculate the prior year's spend for the given product.
Showing the output only for product id 134412:
In year 2020, the previous year's spend is 1800.00 which is the current year's spend for year 2019. Subsequently, in year 2021, 1234.00 which is the previous year's spend is the current year's spend in year 2020. Can you get the gist of how LAG works now?
To better understand the usage of LAG function, check here for more examples.
Finally, we wrap the query above in another CTE called .
Year-on-Year Growth Rate = (Current Year's Spend - Prior Year’s Spend) / Prior Year’s Spend x 100
In the final query, we apply the y-o-y growth rate formula and round the results to 2 nearest decimal places.
Results for product id 134412: