Problem
- Difficulty level : Hard
Table : lyft_rides_log
id | int |
---|---|
user_id | int |
distance | int |
Table : lyft_users
id | int |
---|---|
name | varchar |
top 10 users that have traveled the greatest distance
My Solution
- LEFT JOIN log <- users to GET NAME of user
- AGGREGATE the distance by id(user)
- GROUP BY AGGREGATE -> REMOVE DUPLICATES
- LIMIT 10 (top 10)
/*
- top 10 users
- id | name | total distance traveled
*/
WITH CTE AS (
-- AGGREGATE - SUM BY USER_ID
SELECT
l.user_id,
u.name,
SUM(distance) OVER (PARTITION BY user_id) AS `total_distance_traveled`
FROM lyft_rides_log l
-- LEFT JOIN : TO GET THE NAME OF USER_ID
LEFT JOIN lyft_users u
ON l.user_id = u.id
-- TO GET THE GREATEST DISTANCE
ORDER BY 3 DESC
)
SELECT
user_id,
name,
total_distance_traveled
FROM CTE
-- REMOVE DUPLICATES
GROUP BY 1,2,3
-- TOP 10 USER RESULTs
LIMIT 10