forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
431 additions
and
19 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
solution/2300-2399/2324.Product Sales Analysis IV/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# [2324. Product Sales Analysis IV](https://leetcode.cn/problems/product-sales-analysis-iv) | ||
|
||
[English Version](/solution/2300-2399/2324.Product%20Sales%20Analysis%20IV/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>Table: <code>Sales</code></p> | ||
|
||
<pre> | ||
+-------------+-------+ | ||
| Column Name | Type | | ||
+-------------+-------+ | ||
| sale_id | int | | ||
| product_id | int | | ||
| user_id | int | | ||
| quantity | int | | ||
+-------------+-------+ | ||
sale_id is the primary key of this table. | ||
product_id is a foreign key to <code>Product</code> table. | ||
Each row of this table shows the ID of the product and the quantity purchased by a user. | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p>Table: <code>Product</code></p> | ||
|
||
<pre> | ||
+-------------+------+ | ||
| Column Name | Type | | ||
+-------------+------+ | ||
| product_id | int | | ||
| price | int | | ||
+-------------+------+ | ||
product_id is the primary key of this table. | ||
Each row of this table indicates the price of each product. | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<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> | ||
|
||
<p>Return the resulting table in <strong>any order</strong>.</p> | ||
|
||
<p>The query result format is in the following example.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> | ||
Sales table: | ||
+---------+------------+---------+----------+ | ||
| sale_id | product_id | user_id | quantity | | ||
+---------+------------+---------+----------+ | ||
| 1 | 1 | 101 | 10 | | ||
| 2 | 3 | 101 | 7 | | ||
| 3 | 1 | 102 | 9 | | ||
| 4 | 2 | 102 | 6 | | ||
| 5 | 3 | 102 | 10 | | ||
| 6 | 1 | 102 | 6 | | ||
+---------+------------+---------+----------+ | ||
Product table: | ||
+------------+-------+ | ||
| product_id | price | | ||
+------------+-------+ | ||
| 1 | 10 | | ||
| 2 | 25 | | ||
| 3 | 15 | | ||
+------------+-------+ | ||
<strong>Output:</strong> | ||
+---------+------------+ | ||
| user_id | product_id | | ||
+---------+------------+ | ||
| 101 | 3 | | ||
| 102 | 1 | | ||
| 102 | 2 | | ||
| 102 | 3 | | ||
+---------+------------+ | ||
<strong>Explanation:</strong> | ||
User 101: | ||
- Spent 10 * 10 = 100 on product 1. | ||
- Spent 7 * 15 = 105 on product 3. | ||
User 101 spent the most money on product 3. | ||
User 102: | ||
- Spent (9 + 7) * 10 = 150 on product 1. | ||
- Spent 6 * 25 = 150 on product 2. | ||
- Spent 10 * 15 = 150 on product 3. | ||
User 102 spent the most money on products 1, 2, and 3. | ||
</pre> | ||
|
||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **SQL** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```sql | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
102 changes: 102 additions & 0 deletions
102
solution/2300-2399/2324.Product Sales Analysis IV/README_EN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# [2324. Product Sales Analysis IV](https://leetcode.com/problems/product-sales-analysis-iv) | ||
|
||
[中文文档](/solution/2300-2399/2324.Product%20Sales%20Analysis%20IV/README.md) | ||
|
||
## Description | ||
|
||
<p>Table: <code>Sales</code></p> | ||
|
||
<pre> | ||
+-------------+-------+ | ||
| Column Name | Type | | ||
+-------------+-------+ | ||
| sale_id | int | | ||
| product_id | int | | ||
| user_id | int | | ||
| quantity | int | | ||
+-------------+-------+ | ||
sale_id is the primary key of this table. | ||
product_id is a foreign key to <code>Product</code> table. | ||
Each row of this table shows the ID of the product and the quantity purchased by a user. | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p>Table: <code>Product</code></p> | ||
|
||
<pre> | ||
+-------------+------+ | ||
| Column Name | Type | | ||
+-------------+------+ | ||
| product_id | int | | ||
| price | int | | ||
+-------------+------+ | ||
product_id is the primary key of this table. | ||
Each row of this table indicates the price of each product. | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<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> | ||
|
||
<p>Return the resulting table in <strong>any order</strong>.</p> | ||
|
||
<p>The query result format is in the following example.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> | ||
Sales table: | ||
+---------+------------+---------+----------+ | ||
| sale_id | product_id | user_id | quantity | | ||
+---------+------------+---------+----------+ | ||
| 1 | 1 | 101 | 10 | | ||
| 2 | 3 | 101 | 7 | | ||
| 3 | 1 | 102 | 9 | | ||
| 4 | 2 | 102 | 6 | | ||
| 5 | 3 | 102 | 10 | | ||
| 6 | 1 | 102 | 6 | | ||
+---------+------------+---------+----------+ | ||
Product table: | ||
+------------+-------+ | ||
| product_id | price | | ||
+------------+-------+ | ||
| 1 | 10 | | ||
| 2 | 25 | | ||
| 3 | 15 | | ||
+------------+-------+ | ||
<strong>Output:</strong> | ||
+---------+------------+ | ||
| user_id | product_id | | ||
+---------+------------+ | ||
| 101 | 3 | | ||
| 102 | 1 | | ||
| 102 | 2 | | ||
| 102 | 3 | | ||
+---------+------------+ | ||
<strong>Explanation:</strong> | ||
User 101: | ||
- Spent 10 * 10 = 100 on product 1. | ||
- Spent 7 * 15 = 105 on product 3. | ||
User 101 spent the most money on product 3. | ||
User 102: | ||
- Spent (9 + 7) * 10 = 150 on product 1. | ||
- Spent 6 * 25 = 150 on product 2. | ||
- Spent 10 * 15 = 150 on product 3. | ||
User 102 spent the most money on products 1, 2, and 3. | ||
</pre> | ||
|
||
|
||
## Solutions | ||
|
||
<!-- tabs:start --> | ||
|
||
### **SQL** | ||
|
||
```sql | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
101 changes: 101 additions & 0 deletions
101
solution/2300-2399/2329.Product Sales Analysis V/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# [2329. Product Sales Analysis V](https://leetcode.cn/problems/product-sales-analysis-v) | ||
|
||
[English Version](/solution/2300-2399/2329.Product%20Sales%20Analysis%20V/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>Table: <code>Sales</code></p> | ||
|
||
<pre> | ||
+-------------+-------+ | ||
| Column Name | Type | | ||
+-------------+-------+ | ||
| sale_id | int | | ||
| product_id | int | | ||
| user_id | int | | ||
| quantity | int | | ||
+-------------+-------+ | ||
sale_id is the primary key of this table. | ||
product_id is a foreign key to <code>Product</code> table. | ||
Each row of this table shows the ID of the product and the quantity purchased by a user. | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p>Table: <code>Product</code></p> | ||
|
||
<pre> | ||
+-------------+------+ | ||
| Column Name | Type | | ||
+-------------+------+ | ||
| product_id | int | | ||
| price | int | | ||
+-------------+------+ | ||
product_id is the primary key of this table. | ||
Each row of this table indicates the price of each product. | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p>Write an SQL query that reports the spending of each user.</p> | ||
|
||
<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> | ||
|
||
<p>The query result format is in the following example.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> | ||
Sales table: | ||
+---------+------------+---------+----------+ | ||
| sale_id | product_id | user_id | quantity | | ||
+---------+------------+---------+----------+ | ||
| 1 | 1 | 101 | 10 | | ||
| 2 | 2 | 101 | 1 | | ||
| 3 | 3 | 102 | 3 | | ||
| 4 | 3 | 102 | 2 | | ||
| 5 | 2 | 103 | 3 | | ||
+---------+------------+---------+----------+ | ||
Product table: | ||
+------------+-------+ | ||
| product_id | price | | ||
+------------+-------+ | ||
| 1 | 10 | | ||
| 2 | 25 | | ||
| 3 | 15 | | ||
+------------+-------+ | ||
<strong>Output:</strong> | ||
+---------+----------+ | ||
| user_id | spending | | ||
+---------+----------+ | ||
| 101 | 125 | | ||
| 102 | 75 | | ||
| 103 | 75 | | ||
+---------+----------+ | ||
<strong>Explanation:</strong> | ||
User 101 spent 10 * 10 + 1 * 25 = 125. | ||
User 102 spent 3 * 15 + 2 * 15 = 75. | ||
User 103 spent 3 * 25 = 75. | ||
Users 102 and 103 spent the same amount and we break the tie by their ID while user 101 is on the top. | ||
</pre> | ||
|
||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **SQL** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```sql | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
Oops, something went wrong.