|
| 1 | +# [2372. Calculate the Influence of Each Salesperson](https://leetcode.cn/problems/calculate-the-influence-of-each-salesperson) |
| 2 | + |
| 3 | +[English Version](/solution/2300-2399/2372.Calculate%20the%20Influence%20of%20Each%20Salesperson/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Salesperson</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++----------------+---------+ |
| 13 | +| Column Name | Type | |
| 14 | ++----------------+---------+ |
| 15 | +| salesperson_id | int | |
| 16 | +| name | varchar | |
| 17 | ++----------------+---------+ |
| 18 | +sales_person_id is the primary key for this table. |
| 19 | +Each row in this table shows the ID of a salesperson and the price of one unit. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | + |
| 24 | +<p>Table: <code>Customer</code></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | ++----------------+------+ |
| 28 | +| Column Name | Type | |
| 29 | ++----------------+------+ |
| 30 | +| customer_id | int | |
| 31 | +| salesperson_id | int | |
| 32 | ++----------------+------+ |
| 33 | +customer_id is the primary key for this table. |
| 34 | +salesperson_id is a foreign key from the Salesperson table. |
| 35 | +Each row in this table shows the ID of a customer and the ID of the salesperson |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | + |
| 40 | +<p>Table: <code>Sales</code></p> |
| 41 | + |
| 42 | +<pre> |
| 43 | ++-------------+------+ |
| 44 | +| Column Name | Type | |
| 45 | ++-------------+------+ |
| 46 | +| sale_id | int | |
| 47 | +| customer_id | int | |
| 48 | +| price | int | |
| 49 | ++-------------+------+ |
| 50 | +sale_id is the primary key for this table. |
| 51 | +customer_id is a foreign key from the Customer table. |
| 52 | +Each row in this table shows ID of a customer and the price they paid for the sale with sale_id. |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p> </p> |
| 56 | + |
| 57 | +<p>Write an SQL query to report the sum of prices paid by the customers of each salesperson. If a salesperson does not have any customers, the total value should be <code>0</code>.</p> |
| 58 | + |
| 59 | +<p>Return the result table in <strong>any order</strong>.</p> |
| 60 | + |
| 61 | +<p>The query result format is shown in the following example.</p> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | +<p><strong>Example 1:</strong></p> |
| 65 | + |
| 66 | +<pre> |
| 67 | +<strong>Input:</strong> |
| 68 | +Salesperson table: |
| 69 | ++----------------+-------+ |
| 70 | +| salesperson_id | name | |
| 71 | ++----------------+-------+ |
| 72 | +| 1 | Alice | |
| 73 | +| 2 | Bob | |
| 74 | +| 3 | Jerry | |
| 75 | ++----------------+-------+ |
| 76 | +Customer table: |
| 77 | ++-------------+----------------+ |
| 78 | +| customer_id | salesperson_id | |
| 79 | ++-------------+----------------+ |
| 80 | +| 1 | 1 | |
| 81 | +| 2 | 1 | |
| 82 | +| 3 | 2 | |
| 83 | ++-------------+----------------+ |
| 84 | +Sales table: |
| 85 | ++---------+-------------+-------+ |
| 86 | +| sale_id | customer_id | price | |
| 87 | ++---------+-------------+-------+ |
| 88 | +| 1 | 2 | 892 | |
| 89 | +| 2 | 1 | 354 | |
| 90 | +| 3 | 3 | 988 | |
| 91 | +| 4 | 3 | 856 | |
| 92 | ++---------+-------------+-------+ |
| 93 | +<strong>Output:</strong> |
| 94 | ++----------------+-------+-------+ |
| 95 | +| salesperson_id | name | total | |
| 96 | ++----------------+-------+-------+ |
| 97 | +| 1 | Alice | 1246 | |
| 98 | +| 2 | Bob | 1844 | |
| 99 | +| 3 | Jerry | 0 | |
| 100 | ++----------------+-------+-------+ |
| 101 | +<strong>Explanation:</strong> |
| 102 | +Alice is the salesperson for customers 1 and 2. |
| 103 | + - Customer 1 made one purchase with 354. |
| 104 | + - Customer 2 made one purchase with 892. |
| 105 | +The total for Alice is 354 + 892 = 1246. |
| 106 | + |
| 107 | +Bob is the salesperson for customers 3. |
| 108 | + - Customer 1 made one purchase with 988 and 856. |
| 109 | +The total for Bob is 988 + 856 = 1844. |
| 110 | + |
| 111 | +Jerry is not the salesperson of any customer. |
| 112 | +The total for Jerry is 0. |
| 113 | +</pre> |
| 114 | + |
| 115 | +## 解法 |
| 116 | + |
| 117 | +<!-- 这里可写通用的实现逻辑 --> |
| 118 | + |
| 119 | +<!-- tabs:start --> |
| 120 | + |
| 121 | +### **SQL** |
| 122 | + |
| 123 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 124 | + |
| 125 | +```sql |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | +<!-- tabs:end --> |
0 commit comments