美文网首页
Leetcode262. 行程和用户(困难)

Leetcode262. 行程和用户(困难)

作者: kaka22 | 来源:发表于2020-07-10 16:01 被阅读0次

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

    +----+-----------+-----------+---------+--------------------+----------+
    | 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 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

    +----------+--------+--------+
    | 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 |
    +----------+--------+--------+
    

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

    取消率的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)

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

    致谢:
    非常感谢 @cak1erlizhou 详细的提供了这道题和相应的测试用例。

    生成数据

    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');
    

    审题
    取消率的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)
    首先要选出非禁止用户生成的订单 再统计数量 总数很好确定
    然后看被司机或乘客取消的数量 ‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’ 有这三种情况, 因此完成记为1否则即为0即可。

    解答
    选出非禁止用户生成的订单
    开始是这样写的先排除被禁止乘客的订单 其实没必要 因为Role为client的 Users_Id 一定在Client_Id中

    SELECT * 
    FROM Trips AS T
    JOIN Users AS U
    ON T.`Client_Id` = U.`Users_Id`
    WHERE U.`Role` = 'client' and U.Banned = No
    

    这样生成即可

    SELECT *
    FROM Trips AS T
    JOIN Users AS U1 ON (T.client_id = U1.users_id AND U1.banned ='No')
    JOIN Users AS U2 ON (T.driver_id = U2.users_id AND U2.banned ='No')
    

    然后根据Status统计数量

    SELECT T.`Request_at`, COUNT(T.`Status`) AS all_count, SUM(IF(T.`Status` = 'completed', 0, 1)) AS uncom_count
    FROM Trips AS T
    JOIN Users AS U1 ON (T.client_id = U1.users_id AND U1.banned ='No')
    JOIN Users AS U2 ON (T.driver_id = U2.users_id AND U2.banned ='No')
    GROUP BY t.`Request_at`;
    

    注意题目要求需要限制时间范围

      SELECT T.request_at AS `Day`, 
        ROUND(
                SUM(
                    IF(T.STATUS = 'completed',0,1)
                )
                / 
                COUNT(T.STATUS),
                2
        ) AS `Cancellation Rate`
    FROM Trips AS T
    JOIN Users AS U1 ON (T.client_id = U1.users_id AND U1.banned ='No')
    JOIN Users AS U2 ON (T.driver_id = U2.users_id AND U2.banned ='No')
    WHERE T.request_at BETWEEN '2013-10-01' AND '2013-10-03'
    GROUP BY T.request_at
    

    别的方法都复杂了吧

    相关文章

      网友评论

          本文标题:Leetcode262. 行程和用户(困难)

          本文链接:https://www.haomeiwen.com/subject/cljmcktx.html