Написано: 27.03.2023

262. Поездки и пользователи (Trips and Users)

hard

SQL schema

Table: Trips

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| id          | int      |
| client_id   | int      |
| driver_id   | int      |
| city_id     | int      |
| status      | enum     |
| request_at  | date     |     
+-------------+----------+
id is the primary key for this table.
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.
Status is an ENUM type of ('completed', 'cancelled_by_driver', 'cancelled_by_client').

Table: Users

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| users_id    | int      |
| banned      | enum     |
| role        | enum     |
+-------------+----------+
users_id is the primary key for this table.
The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner').
banned is an ENUM type of ('Yes', 'No').

Задание.

Коэффициент отмены рассчитывается путем деления количества отмененных (клиентом или водителем) запросов с действующими пользователями на общее количество запросов с действующими пользователями в этот день.

Напишите SQL-запрос, чтобы найти частоту отмен запросов с действующими пользователями (как клиент, так и драйвер должны быть действующими) каждый день в период с “2013-10-01” по “2013-10-03”. Округлите коэффициент отмены до двух знаков после запятой.

Верните таблицу результатов в любом порядке.

Формат результата запроса приведен в следующем примере.

Пример 1.

Входные данные:

Trips table:
+----+-----------+-----------+---------+---------------------+------------+
| id | client_id | driver_id | city_id | status              | request_at |
+----+-----------+-----------+---------+---------------------+------------+
| 1  | 1         | 10        | 1       | completed           | 2013-10-01 |
| 2  | 2         | 11        | 1       | cancelled_by_driver | 2013-10-01 |
| 3  | 3         | 12        | 6       | completed           | 2013-10-01 |
| 4  | 4         | 13        | 6       | cancelled_by_client | 2013-10-01 |
| 5  | 1         | 10        | 1       | completed           | 2013-10-02 |
| 6  | 2         | 11        | 6       | completed           | 2013-10-02 |
| 7  | 3         | 12        | 6       | completed           | 2013-10-02 |
| 8  | 2         | 12        | 12      | completed           | 2013-10-03 |
| 9  | 3         | 10        | 12      | completed           | 2013-10-03 |
| 10 | 4         | 13        | 12      | cancelled_by_driver | 2013-10-03 |
+----+-----------+-----------+---------+---------------------+------------+
Users table:
+----------+--------+--------+
| users_id | banned | role   |
+----------+--------+--------+
| 1        | No     | client |
| 2        | Yes    | client |
| 3        | No     | client |
| 4        | No     | client |
| 10       | No     | driver |
| 11       | No     | driver |
| 12       | No     | driver |
| 13       | No     | driver |
+----------+--------+--------+

Результат:

+------------+-------------------+
| Day        | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33              |
| 2013-10-02 | 0.00              |
| 2013-10-03 | 0.50              |
+------------+-------------------+

Пояснение:

On 2013-10-01:
  - There were 4 requests in total, 2 of which were canceled.
  - However, the request with Id=2 was made by a banned client (User_Id=2), so it is ignored in the calculation.
  - Hence there are 3 unbanned requests in total, 1 of which was canceled.
  - The Cancellation Rate is (1 / 3) = 0.33
On 2013-10-02:
  - There were 3 requests in total, 0 of which were canceled.
  - The request with Id=6 was made by a banned client, so it is ignored.
  - Hence there are 2 unbanned requests in total, 0 of which were canceled.
  - The Cancellation Rate is (0 / 2) = 0.00
On 2013-10-03:
  - There were 3 requests in total, 1 of which was canceled.
  - The request with Id=8 was made by a banned client, so it is ignored.
  - Hence there are 2 unbanned request in total, 1 of which were canceled.
  - The Cancellation Rate is (1 / 2) = 0.50

Решение.

/* Write your PL/SQL query statement below */
with tr As (
    select t.id,t.request_at,t.status  
    from Trips t Inner Join Users cl On t.client_id=cl.users_id and cl.banned='No' and cl.role='client' 
               Inner Join Users dr On t.driver_id=dr.users_id and dr.banned='No' and dr.role='driver' 
    where 
        to_date(t.request_at,'YYYY-MM-DD') between to_date('20131001','YYYYMMDD') and to_date('20131003','YYYYMMDD') 
)
,tr_all As (
 select request_at,count(id) As cnt
 from tr
 group by request_at   
)
,tr_cancel As (
 select request_at,count(id) As cnt
 from tr 
 where status in ('cancelled_by_driver','cancelled_by_client')   
 group by request_at   
) 
select a.request_at As "Day",Round(coalesce(c.cnt,0)/a.cnt,2) As "Cancellation Rate"    
from tr_all a Left Join tr_cancel c On a.request_at=c.request_at 
order by a.request_at