Trips and Users

hard sql hash map group by

Problem

Given Trips(client_id, driver_id, status, request_at) and Users(users_id, banned), output the cancellation rate of unbanned-user requests for each day between 2013-10-01 and 2013-10-03, rounded to two decimals.

Inputtrips=[(1,10,'completed','10-01'),(2,11,'cancelled_by_driver','10-01'),(3,12,'completed','10-02')]; banned={11}
Output[('10-01', 0.50), ('10-02', 0.00)]
10-01 has 1 trip after filtering banned user 11 (trip with id 2 dropped) → 0/1=0.00. Without banning: 1 cancelled / 2 total = 0.50.

SELECT Request_at AS Day,
       ROUND(SUM(CASE WHEN Status != 'completed' THEN 1 ELSE 0 END) / COUNT(*), 2) AS "Cancellation Rate"
FROM Trips
WHERE Client_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned = 'Yes')
  AND Driver_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned = 'Yes')
  AND Request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY Request_at;
Time: O(T log T) Space: O(D)