Skip to content

1645. Hopper Company Queries II

 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
WITH
  RECURSIVE Calendar AS (
    SELECT 1 AS month
    UNION ALL
    SELECT month + 1
    FROM Calendar
    WHERE month < 12
  )
SELECT
  Calendar.month,
  IFNULL(
    ROUND(
      (
        SELECT COUNT(DISTINCT AcceptedRides.driver_id)
        FROM AcceptedRides
        INNER JOIN Rides
          USING (ride_id)
        WHERE
          YEAR(Rides.requested_at) = '2020'
          AND MONTH(Rides.requested_at) = Calendar.month
      ) / (
        SELECT COUNT(*)
        FROM Drivers
        WHERE YEAR(Drivers.join_date) < '2020'
          OR (
            YEAR(Drivers.join_date) = '2020'
            AND MONTH(Drivers.join_date) <= Calendar.month)
      ) * 100,
      2
    ),
    0
  ) AS working_percentage
FROM Calendar;