|
| 1 | +# [2837. Total Traveled Distance](https://leetcode.cn/problems/total-traveled-distance) |
| 2 | + |
| 3 | +[English Version](/solution/2800-2899/2837.Total%20Traveled%20Distance/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code><font face="monospace">Users</font></code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++-------------+---------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+---------+ |
| 15 | +| user_id | int | |
| 16 | +| name | varchar | |
| 17 | ++-------------+---------+ |
| 18 | +<code>user_id</code> is the primary key for this table. |
| 19 | +Each row of this table contains user id and name. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p>Table: <code>Rides</code></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | ++--------------+------+ |
| 26 | +| Column Name | Type | |
| 27 | ++--------------+------+ |
| 28 | +| ride_id | int | |
| 29 | +| user_id | int | |
| 30 | +| distance | int | |
| 31 | ++--------------+------+ |
| 32 | +ride_id is the primary key for this table. |
| 33 | +Each row of this table contains ride id, user id, and traveled distance. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p>Write an SQL query to calculate the <code>distance</code> traveled by <strong>each user</strong>. If there is a user who hasn't completed any rides, then their <code>distance</code> should be considered as <code>0</code>. Output the <code>user_id</code>, <code>name</code> and total traveled <code>distance</code>.</p> |
| 37 | + |
| 38 | +<p>Return<em> the result table ordered by </em><code>user_id</code><em> in <strong>ascending</strong> order.</em></p> |
| 39 | + |
| 40 | +<p>The query result format is in the following example.</p> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong class="example">Example 1:</strong></p> |
| 44 | + |
| 45 | +<pre> |
| 46 | +<strong>Input:</strong> |
| 47 | +Users table: |
| 48 | ++---------+---------+ |
| 49 | +| user_id | name | |
| 50 | ++---------+---------+ |
| 51 | +| 17 | Addison | |
| 52 | +| 14 | Ethan | |
| 53 | +| 4 | Michael | |
| 54 | +| 2 | Avery | |
| 55 | +| 10 | Eleanor | |
| 56 | ++---------+---------+ |
| 57 | +Rides table: |
| 58 | ++---------+---------+----------+ |
| 59 | +| ride_id | user_id | distance | |
| 60 | ++---------+---------+----------+ |
| 61 | +| 72 | 17 | 160 | |
| 62 | +| 42 | 14 | 161 | |
| 63 | +| 45 | 4 | 59 | |
| 64 | +| 32 | 2 | 197 | |
| 65 | +| 15 | 4 | 357 | |
| 66 | +| 56 | 2 | 196 | |
| 67 | +| 10 | 14 | 25 | |
| 68 | ++---------+---------+----------+ |
| 69 | +<strong>Output:</strong> |
| 70 | ++---------+---------+-------------------+ |
| 71 | +| user_id | name | traveled distance | |
| 72 | ++---------+---------+-------------------+ |
| 73 | +| 2 | Avery | 393 | |
| 74 | +| 4 | Michael | 416 | |
| 75 | +| 10 | Eleanor | 0 | |
| 76 | +| 14 | Ethan | 186 | |
| 77 | +| 17 | Addison | 160 | |
| 78 | ++---------+---------+-------------------+ |
| 79 | +<strong>Explanation:</strong> |
| 80 | +- User id 2 completed two journeys of 197 and 196, resulting in a combined travel distance of 393. |
| 81 | +- User id 4 completed two journeys of 59 and 357, resulting in a combined travel distance of 416. |
| 82 | +- User id 14 completed two journeys of 161 and 25, resulting in a combined travel distance of 186. |
| 83 | +- User id 16 completed only one journey of 160. |
| 84 | +- User id 10 did not complete any journeys, thus the total travel distance remains at 0. |
| 85 | +Returning the table orderd by user_id in ascending order.</pre> |
| 86 | + |
| 87 | +## 解法 |
| 88 | + |
| 89 | +<!-- 这里可写通用的实现逻辑 --> |
| 90 | + |
| 91 | +**方法一:左连接 + 分组求和** |
| 92 | + |
| 93 | +我们可以使用左连接将两张表连接起来,然后使用分组求和的方式计算每个用户的总距离。注意,如果用户没有完成任何骑行,那么他的距离应该被视为 $0$。 |
| 94 | + |
| 95 | +<!-- tabs:start --> |
| 96 | + |
| 97 | +### **SQL** |
| 98 | + |
| 99 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 100 | + |
| 101 | +```sql |
| 102 | +# Write your MySQL query statement below |
| 103 | +SELECT user_id, name, ifnull(sum(distance), 0) AS 'traveled distance' |
| 104 | +FROM |
| 105 | + Users |
| 106 | + LEFT JOIN Rides USING (user_id) |
| 107 | +GROUP BY 1 |
| 108 | +ORDER BY 1; |
| 109 | +``` |
| 110 | + |
| 111 | +<!-- tabs:end --> |
0 commit comments