-
Notifications
You must be signed in to change notification settings - Fork 2
/
1651.sql
48 lines (45 loc) · 1.47 KB
/
1651.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
-- [ LeetCode ] 1651. Hopper Company Queries III
WITH RECURSIVE Months (month) AS (
SELECT 1 AS month
UNION ALL
SELECT month + 1 AS month
FROM Months
WHERE month BETWEEN 1 AND 11
), MonthlyRides (month, monthly_distance, monthly_duration) AS (
SELECT
MONTH(Rides.requested_at) AS month,
SUM(Acceptedrides.ride_distance) AS monthly_distance,
SUM(Acceptedrides.ride_duration) AS monthly_duration
FROM Rides
JOIN AcceptedRides
USING (ride_id)
WHERE YEAR(Rides.requested_at) = 2020
GROUP BY month
), MonthlyTotal (month, monthly_distance, monthly_duration) AS (
SELECT
Months.month,
IFNULL(MonthlyRides.monthly_distance, 0) AS monthly_distance,
IFNULL(MonthlyRides.monthly_duration, 0) AS monthly_duration
FROM Months
LEFT JOIN MonthlyRides
USING (month)
), ThreeMonthlyAverage (month, average_ride_distance, average_ride_duration) AS (
SELECT
JanuaryToOctober.month,
ROUND(AVG(MonthlyTotal.monthly_distance), 2) AS average_ride_distance,
ROUND(AVG(MonthlyTotal.monthly_duration), 2) AS average_ride_duration
FROM (
SELECT month
FROM Months
WHERE month BETWEEN 1 AND 10
) AS JanuaryToOctober
JOIN MonthlyTotal
ON (MonthlyTotal.month - JanuaryToOctober.month) BETWEEN 0 AND 2
GROUP BY JanuaryToOctober.month
)
SELECT
month,
average_ride_distance,
average_ride_duration
FROM ThreeMonthlyAverage
ORDER BY month ASC;