LeetCode--262.行程和用户

    科技2022-07-10  174

    Trips表中存所有出租车的行程信息。每段行程有唯一健 Id,Client_Id 和Driver_Id 是Users表中Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’,‘cancelled_by_client’)。

    Users表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

    写一段 SQL 语句查出2013年10月1日至2013年10月3日期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

    建表

    Create table If Not Exists Trips (Id int,Client_Id int, Driver_Id int, City_Id int, Status ENUM('completed','cancelled_by_driver', 'cancelled_by_client'), Request_at varchar(50)); Create table If Not Exists Users (Users_Id int,Banned varchar(50), Role ENUM('client', 'driver', 'partner')); Truncate table Trips; insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('1', '1', '10', '1', 'completed','2013-10-01'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('2', '2', '11', '1','cancelled_by_driver', '2013-10-01'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('3', '3', '12', '6', 'completed','2013-10-01'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('4', '4', '13', '6','cancelled_by_client', '2013-10-01'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('5', '1', '10', '1', 'completed','2013-10-02'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('6', '2', '11', '6', 'completed','2013-10-02'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('7', '3', '12', '6', 'completed','2013-10-02'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('8', '2', '12', '12', 'completed','2013-10-03'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('9', '3', '10', '12', 'completed','2013-10-03'); insert into Trips (Id, Client_Id, Driver_Id,City_Id, Status, Request_at) values ('10', '4', '13', '12','cancelled_by_driver', '2013-10-03'); Truncate table Users; insert into Users (Users_Id, Banned, Role)values ('1', 'No', 'client'); insert into Users (Users_Id, Banned, Role)values ('2', 'Yes', 'client'); insert into Users (Users_Id, Banned, Role)values ('3', 'No', 'client'); insert into Users (Users_Id, Banned, Role)values ('4', 'No', 'client'); insert into Users (Users_Id, Banned, Role)values ('10', 'No', 'driver'); insert into Users (Users_Id, Banned, Role)values ('11', 'No', 'driver'); insert into Users (Users_Id, Banned, Role)values ('12', 'No', 'driver'); insert into Users (Users_Id, Banned, Role)values ('13', 'No', 'driver');

    题目并没有说清楚顾客到底包不包括司机,按照题目的示例结果显示,其实是包括的司机的,由司机提出的取消请求也应计算进去,我们用Case When,用cancelled%来表示开头是cancelled的所有项,这样就包括了driver和client,然后分母是所有的信息数,条件里限定了时间段,然后是没有被Banned的,然后再保留两位小数即可

    -- 非禁止用户的取消率 -- 非禁止用户:Banned-No -- 取消率:Status-cancelled_by_driver、cancelled_by_client select t.Request_at 'Day', round(sum(case when t.Status like 'cancelled%' then 1 else 0 end) / count(1), 2) 'Cancellation Rate' from Trips t, Users u where u.Users_Id =t.Client_Id and u.Banned = 'No' and t.Request_at between '2013-10-01'and'2013-10-03' GROUP BY t.Request_at

    Processed: 0.067, SQL: 8