|
| 1 | +/* |
| 2 | +Question 262. Trips and Users |
| 3 | +Link: https://leetcode.com/problems/trips-and-users/description/?envType=problem-list-v2&envId=database |
| 4 | +
|
| 5 | +Table: Trips |
| 6 | +
|
| 7 | ++-------------+----------+ |
| 8 | +| Column Name | Type | |
| 9 | ++-------------+----------+ |
| 10 | +| id | int | |
| 11 | +| client_id | int | |
| 12 | +| driver_id | int | |
| 13 | +| city_id | int | |
| 14 | +| status | enum | |
| 15 | +| request_at | varchar | |
| 16 | ++-------------+----------+ |
| 17 | +id is the primary key (column with unique values) for this table. |
| 18 | +The table holds all taxi trips. Each trip has a unique id, while client_id and driver_id are foreign keys to the users_id at the Users table. |
| 19 | +Status is an ENUM (category) type of ('completed', 'cancelled_by_driver', 'cancelled_by_client'). |
| 20 | +
|
| 21 | +Table: Users |
| 22 | +
|
| 23 | ++-------------+----------+ |
| 24 | +| Column Name | Type | |
| 25 | ++-------------+----------+ |
| 26 | +| users_id | int | |
| 27 | +| banned | enum | |
| 28 | +| role | enum | |
| 29 | ++-------------+----------+ |
| 30 | +users_id is the primary key (column with unique values) for this table. |
| 31 | +The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner'). |
| 32 | +banned is an ENUM (category) type of ('Yes', 'No'). |
| 33 | +
|
| 34 | +The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day. |
| 35 | +
|
| 36 | +Write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03" with at least one trip. Round Cancellation Rate to two decimal points. |
| 37 | +
|
| 38 | +Return the result table in any order. |
| 39 | +*/ |
| 40 | + |
| 41 | +SELECT |
| 42 | + t.request_at AS "Day", |
| 43 | + ROUND(COUNT(CASE WHEN t.status = 'cancelled_by_driver' OR t.status = 'cancelled_by_client' THEN 1 END) / COUNT(t.id)::numeric, 2) AS "Cancellation Rate" --noqa: RF05 |
| 44 | +FROM Trips AS t |
| 45 | +LEFT JOIN |
| 46 | + Users AS u1 |
| 47 | + ON t.client_id = u1.users_id |
| 48 | +LEFT JOIN |
| 49 | + Users AS u2 |
| 50 | + ON t.driver_id = u2.users_id |
| 51 | +WHERE u1.banned != 'Yes' AND u2.banned != 'Yes' AND t.request_at BETWEEN '2013-10-01' AND '2013-10-03' |
| 52 | +GROUP BY t.request_at |
0 commit comments