sql练习(行程和路线)

这段SQL查询旨在确定在2013年10月1日至3日期间,未被禁止的用户(乘客和司机)的行程取消率。取消率是通过计算被取消的行程(由司机或客户取消)数量除以总行程数量得出的,结果保留两位小数。查询结果展示每日的取消率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表:Trips

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| Id          | int      |
| Client_Id   | int      |
| Driver_Id   | int      |
| City_Id     | int      |
| Status      | enum     |
| Request_at  | date     |     
+-------------+----------+
Id 是这张表的主键。
这张表中存所有出租车的行程信息。每段行程有唯一 Id ,其中 Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。
Status 是一个表示行程状态的枚举类型,枚举成员为(‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’) 。

 

表:Users

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| Users_Id    | int      |
| Banned      | enum     |
| Role        | enum     |
+-------------+----------+
Users_Id 是这张表的主键。
这张表中存所有用户,每个用户都有一个唯一的 Users_Id ,Role 是一个表示用户身份的枚举类型,枚举成员为 (‘client’, ‘driver’, ‘partner’) 。
Banned 是一个表示用户是否被禁止的枚举类型,枚举成员为 (‘Yes’, ‘No’) 。

 

写一段 SQL 语句查出 "2013-10-01" 至 "2013-10-03" 期间非禁止用户(乘客和司机都必须未被禁止)的取消率。非禁止用户即 Banned 为 No 的用户,禁止用户即 Banned 为 Yes 的用户。

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

返回结果表中的数据可以按任意顺序组织。其中取消率 Cancellation Rate 需要四舍五入保留 两位小数 。

 

查询结果格式如下例所示:

Trips 表:
+----+-----------+-----------+---------+---------------------+------------+
| 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   |
+----------+--------+--------+
| 1        | No     | client |
| 2        | Yes    | client |
| 3        | No     | client |
| 4        | No     | client |
| 10       | No     | driver |
| 11       | No     | driver |
| 12       | No     | driver |
| 13       | No     | driver |
+----------+--------+--------+

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/trips-and-users
 

我的解答(当然在实际运用中要么用存储过程,要么创建临时表,这样可以加快速度):

select d.Request_at as `Day`,ROUND((
    select count(ts.Id) from Trips as ts where ts.Request_at=t.Request_at and (ts.Status='cancelled_by_driver' or ts.Status='cancelled_by_client') and
    exists(
        select 1 from Users where
        Banned='No' and
        (Users_Id=ts.Client_Id)
        and (Role='client')
    )
    and
    exists(
        select 1 from Users where
        Banned='No' and
        (Users_Id=ts.Driver_Id)
        and (Role='driver')
    )
)/count(t.Id),2) as `Cancellation Rate` from (
    select tt.* from Trips as tt where
    exists(
        select 1 from Users where
        Banned='No' and
        (Users_Id=tt.Client_Id)
        and (Role='client')
    )
    and
    exists(
        select 1 from Users where
        Banned='No' and
        (Users_Id=tt.Driver_Id)
        and (Role='driver')
    )
) as t right join (
    select distinct(Request_at) from Trips
) as d 
on d.Request_at=t.Request_at
where (d.Request_at Between cast('2013-10-01' as DATE) and cast('2013-10-03' as DATE))
group by t.Request_at
having count(t.Id)>0
order by t.Request_at

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值