美文网首页
leetcode中的SQL题(十二)

leetcode中的SQL题(十二)

作者: 瓜皮小咸鱼 | 来源:发表于2019-04-03 11:40 被阅读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        |
+------------+-------------------+

答案:

select t.Request_at Day, 
(
round(count(if(status != 'completed', status, null)) / count(status) ,2)
) as Cancellation Rate 
from users u inner join Trips t
on u.Users_Id = t.Client_Id 
and u.Banned = 'No'
where t.Request_at >= '2013-10-01'
and t.Request_at =< '2013-10-03'
group by t.Request_at;

相关文章

  • leetcode中的SQL题(十二)

    Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 U...

  • leetcode中的SQL题(三)

    编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。 例如上述 Employee...

  • leetcode中的SQL题(十三)

    给定一个 salary 表,如下所示,有 m=男性 和 f=女性 的值 。交换所有的 f 和 m 值(例如,将所有...

  • leetcode中的SQL题(八)

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id D...

  • leetcode中的SQL题(七)

    某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客...

  • leetcode中的SQL题(五)

    Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id...

  • leetcode中的SQL题(十)

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。 在...

  • leetcode中的SQL题(四)

    编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次...

  • leetcode中的SQL题(九)

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。...

  • leetcode中的SQL题(十一)

    给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。 ...

网友评论

      本文标题:leetcode中的SQL题(十二)

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