|
1 |
| ---262. Trips and Users |
2 |
| ---The Trips table holds all taxi trips. |
3 |
| ---Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’). |
4 |
| --- |
5 |
| ---+----+-----------+-----------+---------+--------------------+----------+ |
6 |
| ---| Id | Client_Id | Driver_Id | City_Id | Status |Request_at| |
7 |
| ---+----+-----------+-----------+---------+--------------------+----------+ |
8 |
| ---| 1 | 1 | 10 | 1 | completed |2013-10-01| |
9 |
| ---| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01| |
10 |
| ---| 3 | 3 | 12 | 6 | completed |2013-10-01| |
11 |
| ---| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01| |
12 |
| ---| 5 | 1 | 10 | 1 | completed |2013-10-02| |
13 |
| ---| 6 | 2 | 11 | 6 | completed |2013-10-02| |
14 |
| ---| 7 | 3 | 12 | 6 | completed |2013-10-02| |
15 |
| ---| 8 | 2 | 12 | 12 | completed |2013-10-03| |
16 |
| ---| 9 | 3 | 10 | 12 | completed |2013-10-03| |
17 |
| ---| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03| |
18 |
| ---+----+-----------+-----------+---------+--------------------+----------+ |
19 |
| ---The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’). |
20 |
| --- |
21 |
| ---+----------+--------+--------+ |
22 |
| ---| Users_Id | Banned | Role | |
23 |
| ---+----------+--------+--------+ |
24 |
| ---| 1 | No | client | |
25 |
| ---| 2 | Yes | client | |
26 |
| ---| 3 | No | client | |
27 |
| ---| 4 | No | client | |
28 |
| ---| 10 | No | driver | |
29 |
| ---| 11 | No | driver | |
30 |
| ---| 12 | No | driver | |
31 |
| ---| 13 | No | driver | |
32 |
| ---+----------+--------+--------+ |
33 |
| ---Write a SQL query to find the cancellation rate of requests made by unbanned clients between Oct 1, 2013 and Oct 3, 2013. |
34 |
| ---For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places. |
35 |
| --- |
36 |
| ---+------------+-------------------+ |
37 |
| ---| Day | Cancellation Rate | |
38 |
| ---+------------+-------------------+ |
39 |
| ---| 2013-10-01 | 0.33 | |
40 |
| ---| 2013-10-02 | 0.00 | |
41 |
| ---| 2013-10-03 | 0.50 | |
42 |
| ---+------------+-------------------+ |
43 |
| ---Credits: |
44 |
| ---Special thanks to @cak1erlizhou for contributing this question, writing the problem description and adding part of the test cases. |
45 |
| - |
46 | 1 | select t.Request_at as Day,
|
47 | 2 | round(sum(case when t.Status like 'cancelled_%' then 1 else 0 end)/count(*), 2) as 'Cancellation Rate'
|
48 | 3 | from Trips t
|
|
0 commit comments