8
8
9
9
<!-- problem:start -->
10
10
11
- # [ 3214. Year on Year Growth Rate 🔒] ( https://leetcode.cn/problems/year-on-year-growth-rate )
11
+ # [ 3214. 同比增长率 🔒] ( https://leetcode.cn/problems/year-on-year-growth-rate )
12
12
13
13
[ English Version] ( /solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md )
14
14
15
15
## 题目描述
16
16
17
17
<!-- description:start -->
18
18
19
- <p >Table: <code >user_transactions</code ></p >
19
+ <p >表: <code >user_transactions</code ></p >
20
20
21
21
<pre >
22
22
+------------------+----------+
@@ -27,33 +27,34 @@ tags:
27
27
| spend | decimal |
28
28
| transaction_date | datetime |
29
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.
30
+ transaction_id 列唯一标识了表中的每一列。
31
+ 这张表的每一行含有交易 ID,产品 ID,总花费以及交易日期。
32
32
</pre >
33
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 >
34
+ <p >编写一个解决方案来计算 <strong >每个产品 </strong > 总支出的 <strong >同比增长率 </strong >。 </p >
35
35
36
- <p >The result table should include the following columns: </p >
36
+ <p >结果表应该包含以下列: </p >
37
37
38
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>
39
+ <li><code>year</code>:交易的年份。 </li>
40
+ <li><code>product_id</code>:产品的 ID。 </li>
41
+ <li><code>curr_year_spend</code>:当年的总支出。 </li>
42
+ <li><code>prev_year_spend</code>:上一年的总支出。 </li>
43
+ <li><code>yoy_rate</code>:同比增速百分比,四舍五入至小数点后 2 位。 </li>
44
44
</ul >
45
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 >
46
+ <p >返回结果表以   ; <code >product_id</code >, <code >year</code >& nbsp ; < strong >升序 </strong > 排序。 </p >
47
47
48
- <p >The result format is in the following example. </p >
48
+ <p >结果格式如下所示。 </p >
49
49
50
50
<p >  ; </p >
51
- <p ><strong class =" example " >Example:</strong ></p >
51
+
52
+ <p ><strong class =" example " >示例:</strong ></p >
52
53
53
54
<div class =" example-block " >
54
- <p ><strong >Input: </strong ></p >
55
+ <p ><strong >输入: </strong ></p >
55
56
56
- <p ><code >user_transactions</code > table: </p >
57
+ <p ><code >user_transactions</code > 表: </p >
57
58
58
59
<pre class =" example-io " >
59
60
+----------------+------------+---------+---------------------+
@@ -66,7 +67,7 @@ Each row of this table contains the transaction ID, product ID, the spend amount
66
67
+----------------+------------+---------+---------------------+
67
68
</pre >
68
69
69
- <p ><strong >Output: </strong ></p >
70
+ <p ><strong >输出: </strong ></p >
70
71
71
72
<pre class =" example-io " >
72
73
+------+------------+----------------+----------------+----------+
@@ -79,44 +80,44 @@ Each row of this table contains the transaction ID, product ID, the spend amount
79
80
+------+------------+----------------+----------------+----------+
80
81
</pre >
81
82
82
- <p ><strong >Explanation: </strong ></p >
83
+ <p ><strong >解释: </strong ></p >
83
84
84
85
<ul >
85
- <li>For product ID 123424:
86
+ <li>对于产品 ID 123424:
86
87
<ul>
87
- <li>In 2019:
88
+ <li>在 2019:
88
89
<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>
90
+ <li>当年的支出是 1500.60</li>
91
+ <li>没有上一年支出的记录 </li>
92
+ <li>同比增长率: NULL</li>
92
93
</ul>
93
94
</li>
94
- <li>In 2020:
95
+ <li>在 2020:
95
96
<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>
97
+ <li>当年的支出是 1000.20</li>
98
+ <li>上一年的支出是 1500.60</li>
99
+ <li>同比增长率: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li>
99
100
</ul>
100
101
</li>
101
- <li>In 2021:
102
+ <li>在 2021:
102
103
<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>
104
+ <li>当年的支出是 1246.44</li>
105
+ <li>上一年的支出是 1000.20</li>
106
+ <li>同比增长率: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li>
106
107
</ul>
107
108
</li>
108
- <li>In 2022:
109
+ <li>在 2022:
109
110
<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>
111
+ <li>当年的支出是 2145.32</li>
112
+ <li>上一年的支出是 1246.44</li>
113
+ <li>同比增长率: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li>
113
114
</ul>
114
115
</li>
115
116
</ul>
116
117
</li>
117
118
</ul >
118
119
119
- <p ><strong >Note: </strong > Output table is ordered by <code >product_id</code > and <code >year</code > in ascending order. </p >
120
+ <p ><strong >注意: </strong >输出表以 & nbsp ; <code >product_id</code > 和 & nbsp ; <code >year</code >& nbsp ; 升序排序。 </p >
120
121
</div >
121
122
122
123
<!-- description:end -->
@@ -125,9 +126,9 @@ Each row of this table contains the transaction ID, product ID, the spend amount
125
126
126
127
<!-- solution:start -->
127
128
128
- ### 方法一:分组统计 + 窗口函数
129
+ ### 方法一:分组统计 + 左连接
129
130
130
- 我们可以先按照年份和产品 ID 进行分组统计每个产品每年的总花费,记录在 ` T ` 表中。然后使用窗口函数 ` LAG ` 计算出上一年的总花费,记录在 ` S ` 表中。最后根据公式计算出年增长率 。
131
+ 我们可以先按照 ` product_id ` 和 ` year(transaction_date) ` 进行分组统计,然后使用左连接将当前年份的统计结果与上一年份的统计结果进行关联,最后计算年同比增长率 。
131
132
132
133
<!-- tabs:start -->
133
134
@@ -137,25 +138,21 @@ Each row of this table contains the transaction ID, product ID, the spend amount
137
138
# Write your MySQL query statement below
138
139
WITH
139
140
T AS (
140
- SELECT YEAR(transaction_date) year, product_id, SUM (spend) tot_spend
141
+ SELECT product_id, YEAR(transaction_date) year, SUM (spend) curr_year_spend
141
142
FROM user_transactions
142
143
GROUP BY 1 , 2
143
144
),
144
145
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
146
+ SELECT t1 .year , t1 .product_id , t1 .curr_year_spend , t2 .curr_year_spend prev_year_spend
147
+ FROM
148
+ T t1
149
+ LEFT JOIN T t2 ON t1 .product_id = t2 .product_id AND t1 .year = t2 .year + 1
154
150
)
155
151
SELECT
156
152
* ,
157
153
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100 , 2 ) yoy_rate
158
- FROM S;
154
+ FROM S
155
+ ORDER BY 2 , 1 ;
159
156
```
160
157
161
158
<!-- tabs:end -->
0 commit comments