本文主要是介绍262. Trips and Users - 行程和用户 <Hard>,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 |
+------------+-------------------+
分析:主表 - Trips 表,
被司机或乘客取消的非禁止用户生成的订单数量:'cancelled_by_client' or 'cancelled_by_driver' 或 like 'cancelled_%'
非禁止用户生成的订单总数:需要保证trip表司机与乘客非ban!需要两个User表实例关联对应字段~
ps:注意sum与count函数的区别 !
sum(),求和累加,不计算空值;
count(),求行数,空值认为没有此项;
# count(IF(a.Status = 'cancelled_by_client' or a.Status = 'cancelled_by_driver' ,1, NULL))
# count(IF(a.Status like 'cancelled_%' ,1, NULL))
# sum(IF(a.Status like 'cancelled_%' ,1, 0)
# count(case when a.Status like 'cancelled_%' then 1 else NULL end)
# sum(a.Status != 'completed')select Request_at as Day,round(sum(IF(a.Status like 'cancelled_%' ,1, 0))/count(*),2) as `Cancellation Rate`
from Trips a
left join Users b on b.Users_Id = a.Client_Id
left join Users c on c.Users_Id = a.Driver_Id
where b.Banned = 'No' and c.Banned = 'No'
and a.Request_at between '2013-10-01' and '2013-10-03'
group by a.Request_at
这篇关于262. Trips and Users - 行程和用户 <Hard>的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!