|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Hard |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md |
| 5 | +tags: |
| 6 | + - Database |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3214. Year on Year Growth Rate 🔒](https://leetcode.com/problems/year-on-year-growth-rate) |
| 12 | + |
| 13 | +[中文文档](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>user_transactions</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++------------------+----------+ |
| 23 | +| Column Name | Type | |
| 24 | ++------------------+----------+ |
| 25 | +| transaction_id | integer | |
| 26 | +| product_id | integer | |
| 27 | +| spend | decimal | |
| 28 | +| transaction_date | datetime | |
| 29 | ++------------------+----------+ |
| 30 | +The transaction_id column uniquely identifies each row in this table. |
| 31 | +Each row of this table contains the transaction ID, product ID, the spend amount, and the transaction date. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p>Write a solution to calculate the <strong>year-on-year growth rate</strong> for the total spend <strong>for each product</strong>.</p> |
| 35 | + |
| 36 | +<p>The result table should include the following columns:</p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li><code>year</code>: The year of the transaction.</li> |
| 40 | + <li><code>product_id</code>: The ID of the product.</li> |
| 41 | + <li><code>curr_year_spend</code>: The total spend for the current year.</li> |
| 42 | + <li><code>prev_year_spend</code>: The total spend for the previous year.</li> |
| 43 | + <li><code>yoy_rate</code>: The year-on-year growth rate percentage, rounded to <code>2</code> decimal places.</li> |
| 44 | +</ul> |
| 45 | + |
| 46 | +<p>Return <em>the result table ordered by</em> <code>product_id</code>,<code>year</code> <em>in <strong>ascending</strong> order</em>.</p> |
| 47 | + |
| 48 | +<p>The result format is in the following example.</p> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong class="example">Example:</strong></p> |
| 52 | + |
| 53 | +<div class="example-block"> |
| 54 | +<p><strong>Input:</strong></p> |
| 55 | + |
| 56 | +<p><code>user_transactions</code> table:</p> |
| 57 | + |
| 58 | +<pre class="example-io"> |
| 59 | ++----------------+------------+---------+---------------------+ |
| 60 | +| transaction_id | product_id | spend | transaction_date | |
| 61 | ++----------------+------------+---------+---------------------+ |
| 62 | +| 1341 | 123424 | 1500.60 | 2019-12-31 12:00:00 | |
| 63 | +| 1423 | 123424 | 1000.20 | 2020-12-31 12:00:00 | |
| 64 | +| 1623 | 123424 | 1246.44 | 2021-12-31 12:00:00 | |
| 65 | +| 1322 | 123424 | 2145.32 | 2022-12-31 12:00:00 | |
| 66 | ++----------------+------------+---------+---------------------+ |
| 67 | +</pre> |
| 68 | + |
| 69 | +<p><strong>Output:</strong></p> |
| 70 | + |
| 71 | +<pre class="example-io"> |
| 72 | ++------+------------+----------------+----------------+----------+ |
| 73 | +| year | product_id | curr_year_spend| prev_year_spend| yoy_rate | |
| 74 | ++------+------------+----------------+----------------+----------+ |
| 75 | +| 2019 | 123424 | 1500.60 | NULL | NULL | |
| 76 | +| 2020 | 123424 | 1000.20 | 1500.60 | -33.35 | |
| 77 | +| 2021 | 123424 | 1246.44 | 1000.20 | 24.62 | |
| 78 | +| 2022 | 123424 | 2145.32 | 1246.44 | 72.12 | |
| 79 | ++------+------------+----------------+----------------+----------+ |
| 80 | +</pre> |
| 81 | + |
| 82 | +<p><strong>Explanation:</strong></p> |
| 83 | + |
| 84 | +<ul> |
| 85 | + <li>For product ID 123424: |
| 86 | + <ul> |
| 87 | + <li>In 2019: |
| 88 | + <ul> |
| 89 | + <li>Current year's spend is 1500.60</li> |
| 90 | + <li>No previous year's spend recorded</li> |
| 91 | + <li>YoY growth rate: NULL</li> |
| 92 | + </ul> |
| 93 | + </li> |
| 94 | + <li>In 2020: |
| 95 | + <ul> |
| 96 | + <li>Current year's spend is 1000.20</li> |
| 97 | + <li>Previous year's spend is 1500.60</li> |
| 98 | + <li>YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li> |
| 99 | + </ul> |
| 100 | + </li> |
| 101 | + <li>In 2021: |
| 102 | + <ul> |
| 103 | + <li>Current year's spend is 1246.44</li> |
| 104 | + <li>Previous year's spend is 1000.20</li> |
| 105 | + <li>YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li> |
| 106 | + </ul> |
| 107 | + </li> |
| 108 | + <li>In 2022: |
| 109 | + <ul> |
| 110 | + <li>Current year's spend is 2145.32</li> |
| 111 | + <li>Previous year's spend is 1246.44</li> |
| 112 | + <li>YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li> |
| 113 | + </ul> |
| 114 | + </li> |
| 115 | + </ul> |
| 116 | + </li> |
| 117 | +</ul> |
| 118 | + |
| 119 | +<p><strong>Note:</strong> Output table is ordered by <code>product_id</code> and <code>year</code> in ascending order.</p> |
| 120 | +</div> |
| 121 | + |
| 122 | +<!-- description:end --> |
| 123 | + |
| 124 | +## Solutions |
| 125 | + |
| 126 | +<!-- solution:start --> |
| 127 | + |
| 128 | +### Solution 1: Grouping Statistics + Window Function |
| 129 | + |
| 130 | +We can first group by year and product ID to calculate the total cost of each product every year, recorded in table `T`. Then, use the window function `LAG` to calculate the total cost of the previous year, recorded in table `S`. Finally, calculate the annual growth rate based on the formula. |
| 131 | + |
| 132 | +<!-- tabs:start --> |
| 133 | + |
| 134 | +#### MySQL |
| 135 | + |
| 136 | +```sql |
| 137 | +# Write your MySQL query statement below |
| 138 | +WITH |
| 139 | + T AS ( |
| 140 | + SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend |
| 141 | + FROM user_transactions |
| 142 | + GROUP BY 1, 2 |
| 143 | + ), |
| 144 | + S AS ( |
| 145 | + SELECT |
| 146 | + year, |
| 147 | + product_id, |
| 148 | + tot_spend curr_year_spend, |
| 149 | + LAG(tot_spend) OVER ( |
| 150 | + PARTITION BY product_id |
| 151 | + ORDER BY year |
| 152 | + ) prev_year_spend |
| 153 | + FROM T |
| 154 | + ) |
| 155 | +SELECT |
| 156 | + *, |
| 157 | + ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate |
| 158 | +FROM S; |
| 159 | +``` |
| 160 | + |
| 161 | +<!-- tabs:end --> |
| 162 | + |
| 163 | +<!-- solution:end --> |
| 164 | + |
| 165 | +<!-- problem:end --> |
0 commit comments