Skip to content

1651. Hopper Company Queries III 👎

 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
49
WITH
  RECURSIVE Calendar AS (
    SELECT 2 AS MONTH
    UNION ALL
    SELECT MONTH + 1
    FROM Calendar
    WHERE MONTH < 11
  )
SELECT
  Calendar.month - 1 AS MONTH,
  IFNULL(
    ROUND(
      (
        SELECT SUM(AcceptedRides.ride_distance)
        FROM Rides
        INNER JOIN AcceptedRides
          USING (ride_id)
        WHERE
          YEAR(Rides.requested_at) = '2020'
          AND MONTH(Rides.requested_at) IN (
            Calendar.month - 1,
            Calendar.month,
            Calendar.month + 1
          )
      ) / 3,
      2
    ),
    0
  ) AS average_ride_distance,
  IFNULL(
    ROUND(
      (
        SELECT SUM(AcceptedRides.ride_duration)
        FROM Rides
        INNER JOIN AcceptedRides
          USING (ride_id)
        WHERE
          YEAR(Rides.requested_at) = '2020'
          AND MONTH(Rides.requested_at) IN (
            Calendar.month - 1,
            Calendar.month,
            Calendar.month + 1
          )
      ) / 3,
      2
    ),
    0
  ) AS average_ride_duration
FROM Calendar;