【Leetcode】数据库题目:行程和用户

本文介绍了一种使用SQL查询特定日期范围内非禁止用户的出租车行程取消率的方法。通过联接Trips和Users表,筛选出有效行程,并计算每日取消率。

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

解答:

select
    x.Request_at  Day ,
    round(x.rank/y.num,2) 'Cancellation Rate'
from
(
select 
    t.Request_at,max(t.rank) as rank
from
(
select
    a.Request_at,
    (
        @cancel := 
            if(
                @pre_date=a.Request_at,
                if(a.Status='cancelled_by_client' or a.Status='cancelled_by_driver',@cancel+1,@cancel),
                if(a.Status='cancelled_by_client' or a.Status='cancelled_by_driver',@cancel:=1,@cancel:=0)
            )
    ) rank,
    @pre_date := a.Request_at
from
    (
        select 
            Request_at,Status
        from
            Trips
        where 
            Request_at >='2013-10-01' and Request_at <='2013-10-03' 
        and
            Client_Id in 
            (
                select 
                    Users_Id
                from 
                    Users
                where Banned = 'No' and Role = 'client'
            )
        order by
            Request_at asc
    ) as a,(select @pre_date:=null,@cancel:=0) as b 
) as t
group by 
    t.Request_at
) as x

join (
    select 
        Request_at,count(Client_Id) as num
    from
        Trips
    where 
        Request_at >='2013-10-01' and Request_at <='2013-10-03' 
    and
        Client_Id in 
        (
            select 
                Users_Id
            from 
                Users
            where Banned = 'No' and Role = 'client'
        ) 
    group by
        Request_at
) as y on x.Request_at=y.Request_at 






 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值