Skip to content

Commit 87da3d6

Browse files
committed
feat: update lc problems
1 parent 85f6a37 commit 87da3d6

File tree

8 files changed

+431
-19
lines changed

8 files changed

+431
-19
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [2324. Product Sales Analysis IV](https://leetcode.cn/problems/product-sales-analysis-iv)
2+
3+
[English Version](/solution/2300-2399/2324.Product%20Sales%20Analysis%20IV/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Sales</code></p>
10+
11+
<pre>
12+
+-------------+-------+
13+
| Column Name | Type |
14+
+-------------+-------+
15+
| sale_id | int |
16+
| product_id | int |
17+
| user_id | int |
18+
| quantity | int |
19+
+-------------+-------+
20+
sale_id is the primary key of this table.
21+
product_id is a foreign key to <code>Product</code> table.
22+
Each row of this table shows the ID of the product and the quantity purchased by a user.
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
27+
<p>Table: <code>Product</code></p>
28+
29+
<pre>
30+
+-------------+------+
31+
| Column Name | Type |
32+
+-------------+------+
33+
| product_id | int |
34+
| price | int |
35+
+-------------+------+
36+
product_id is the primary key of this table.
37+
Each row of this table indicates the price of each product.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p>Write an SQL query that reports for each user the product id on which the user spent the most money. In case the same user spent the most money on two or more products, report all of them.</p>
43+
44+
<p>Return the resulting table in <strong>any order</strong>.</p>
45+
46+
<p>The query result format is in the following example.</p>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Example 1:</strong></p>
50+
51+
<pre>
52+
<strong>Input:</strong>
53+
Sales table:
54+
+---------+------------+---------+----------+
55+
| sale_id | product_id | user_id | quantity |
56+
+---------+------------+---------+----------+
57+
| 1 | 1 | 101 | 10 |
58+
| 2 | 3 | 101 | 7 |
59+
| 3 | 1 | 102 | 9 |
60+
| 4 | 2 | 102 | 6 |
61+
| 5 | 3 | 102 | 10 |
62+
| 6 | 1 | 102 | 6 |
63+
+---------+------------+---------+----------+
64+
Product table:
65+
+------------+-------+
66+
| product_id | price |
67+
+------------+-------+
68+
| 1 | 10 |
69+
| 2 | 25 |
70+
| 3 | 15 |
71+
+------------+-------+
72+
<strong>Output:</strong>
73+
+---------+------------+
74+
| user_id | product_id |
75+
+---------+------------+
76+
| 101 | 3 |
77+
| 102 | 1 |
78+
| 102 | 2 |
79+
| 102 | 3 |
80+
+---------+------------+
81+
<strong>Explanation:</strong>
82+
User 101:
83+
- Spent 10 * 10 = 100 on product 1.
84+
- Spent 7 * 15 = 105 on product 3.
85+
User 101 spent the most money on product 3.
86+
User 102:
87+
- Spent (9 + 7) * 10 = 150 on product 1.
88+
- Spent 6 * 25 = 150 on product 2.
89+
- Spent 10 * 15 = 150 on product 3.
90+
User 102 spent the most money on products 1, 2, and 3.
91+
</pre>
92+
93+
94+
## 解法
95+
96+
<!-- 这里可写通用的实现逻辑 -->
97+
98+
<!-- tabs:start -->
99+
100+
### **SQL**
101+
102+
<!-- 这里可写当前语言的特殊实现逻辑 -->
103+
104+
```sql
105+
106+
```
107+
108+
<!-- tabs:end -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [2324. Product Sales Analysis IV](https://leetcode.com/problems/product-sales-analysis-iv)
2+
3+
[中文文档](/solution/2300-2399/2324.Product%20Sales%20Analysis%20IV/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Sales</code></p>
8+
9+
<pre>
10+
+-------------+-------+
11+
| Column Name | Type |
12+
+-------------+-------+
13+
| sale_id | int |
14+
| product_id | int |
15+
| user_id | int |
16+
| quantity | int |
17+
+-------------+-------+
18+
sale_id is the primary key of this table.
19+
product_id is a foreign key to <code>Product</code> table.
20+
Each row of this table shows the ID of the product and the quantity purchased by a user.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p>Table: <code>Product</code></p>
26+
27+
<pre>
28+
+-------------+------+
29+
| Column Name | Type |
30+
+-------------+------+
31+
| product_id | int |
32+
| price | int |
33+
+-------------+------+
34+
product_id is the primary key of this table.
35+
Each row of this table indicates the price of each product.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p>Write an SQL query that reports for each user the product id on which the user spent the most money. In case the same user spent the most money on two or more products, report all of them.</p>
41+
42+
<p>Return the resulting table in <strong>any order</strong>.</p>
43+
44+
<p>The query result format is in the following example.</p>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Example 1:</strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong>
51+
Sales table:
52+
+---------+------------+---------+----------+
53+
| sale_id | product_id | user_id | quantity |
54+
+---------+------------+---------+----------+
55+
| 1 | 1 | 101 | 10 |
56+
| 2 | 3 | 101 | 7 |
57+
| 3 | 1 | 102 | 9 |
58+
| 4 | 2 | 102 | 6 |
59+
| 5 | 3 | 102 | 10 |
60+
| 6 | 1 | 102 | 6 |
61+
+---------+------------+---------+----------+
62+
Product table:
63+
+------------+-------+
64+
| product_id | price |
65+
+------------+-------+
66+
| 1 | 10 |
67+
| 2 | 25 |
68+
| 3 | 15 |
69+
+------------+-------+
70+
<strong>Output:</strong>
71+
+---------+------------+
72+
| user_id | product_id |
73+
+---------+------------+
74+
| 101 | 3 |
75+
| 102 | 1 |
76+
| 102 | 2 |
77+
| 102 | 3 |
78+
+---------+------------+
79+
<strong>Explanation:</strong>
80+
User 101:
81+
- Spent 10 * 10 = 100 on product 1.
82+
- Spent 7 * 15 = 105 on product 3.
83+
User 101 spent the most money on product 3.
84+
User 102:
85+
- Spent (9 + 7) * 10 = 150 on product 1.
86+
- Spent 6 * 25 = 150 on product 2.
87+
- Spent 10 * 15 = 150 on product 3.
88+
User 102 spent the most money on products 1, 2, and 3.
89+
</pre>
90+
91+
92+
## Solutions
93+
94+
<!-- tabs:start -->
95+
96+
### **SQL**
97+
98+
```sql
99+
100+
```
101+
102+
<!-- tabs:end -->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2329. Product Sales Analysis V](https://leetcode.cn/problems/product-sales-analysis-v)
2+
3+
[English Version](/solution/2300-2399/2329.Product%20Sales%20Analysis%20V/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Sales</code></p>
10+
11+
<pre>
12+
+-------------+-------+
13+
| Column Name | Type |
14+
+-------------+-------+
15+
| sale_id | int |
16+
| product_id | int |
17+
| user_id | int |
18+
| quantity | int |
19+
+-------------+-------+
20+
sale_id is the primary key of this table.
21+
product_id is a foreign key to <code>Product</code> table.
22+
Each row of this table shows the ID of the product and the quantity purchased by a user.
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
27+
<p>Table: <code>Product</code></p>
28+
29+
<pre>
30+
+-------------+------+
31+
| Column Name | Type |
32+
+-------------+------+
33+
| product_id | int |
34+
| price | int |
35+
+-------------+------+
36+
product_id is the primary key of this table.
37+
Each row of this table indicates the price of each product.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p>Write an SQL query that reports the spending of each user.</p>
43+
44+
<p>Return the resulting table ordered by <code>spending</code> in <strong>descending order</strong>. In case of a tie, order them by <code>user_id</code>.</p>
45+
46+
<p>The query result format is in the following example.</p>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Example 1:</strong></p>
50+
51+
<pre>
52+
<strong>Input:</strong>
53+
Sales table:
54+
+---------+------------+---------+----------+
55+
| sale_id | product_id | user_id | quantity |
56+
+---------+------------+---------+----------+
57+
| 1 | 1 | 101 | 10 |
58+
| 2 | 2 | 101 | 1 |
59+
| 3 | 3 | 102 | 3 |
60+
| 4 | 3 | 102 | 2 |
61+
| 5 | 2 | 103 | 3 |
62+
+---------+------------+---------+----------+
63+
Product table:
64+
+------------+-------+
65+
| product_id | price |
66+
+------------+-------+
67+
| 1 | 10 |
68+
| 2 | 25 |
69+
| 3 | 15 |
70+
+------------+-------+
71+
<strong>Output:</strong>
72+
+---------+----------+
73+
| user_id | spending |
74+
+---------+----------+
75+
| 101 | 125 |
76+
| 102 | 75 |
77+
| 103 | 75 |
78+
+---------+----------+
79+
<strong>Explanation:</strong>
80+
User 101 spent 10 * 10 + 1 * 25 = 125.
81+
User 102 spent 3 * 15 + 2 * 15 = 75.
82+
User 103 spent 3 * 25 = 75.
83+
Users 102 and 103 spent the same amount and we break the tie by their ID while user 101 is on the top.
84+
</pre>
85+
86+
87+
## 解法
88+
89+
<!-- 这里可写通用的实现逻辑 -->
90+
91+
<!-- tabs:start -->
92+
93+
### **SQL**
94+
95+
<!-- 这里可写当前语言的特殊实现逻辑 -->
96+
97+
```sql
98+
99+
```
100+
101+
<!-- tabs:end -->

0 commit comments

Comments
 (0)