为有牺牲多壮志,敢教日月换新天。

[SQL]LeetCode262.行程和用户 | Trips and Users

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10227593.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

SQL架构

 1 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))
 2 Create table If Not Exists Users (Users_Id int, Banned varchar(50), Role ENUM('client', 'driver', 'partner'))
 3 Truncate table Trips
 4 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('1', '1', '10', '1', 'completed', '2013-10-01')
 5 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('2', '2', '11', '1', 'cancelled_by_driver', '2013-10-01')
 6 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('3', '3', '12', '6', 'completed', '2013-10-01')
 7 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('4', '4', '13', '6', 'cancelled_by_client', '2013-10-01')
 8 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('5', '1', '10', '1', 'completed', '2013-10-02')
 9 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('6', '2', '11', '6', 'completed', '2013-10-02')
10 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('7', '3', '12', '6', 'completed', '2013-10-02')
11 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('8', '2', '12', '12', 'completed', '2013-10-03')
12 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('9', '3', '10', '12', 'completed', '2013-10-03')
13 insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('10', '4', '13', '12', 'cancelled_by_driver', '2013-10-03')
14 Truncate table Users
15 insert into Users (Users_Id, Banned, Role) values ('1', 'No', 'client')
16 insert into Users (Users_Id, Banned, Role) values ('2', 'Yes', 'client')
17 insert into Users (Users_Id, Banned, Role) values ('3', 'No', 'client')
18 insert into Users (Users_Id, Banned, Role) values ('4', 'No', 'client')
19 insert into Users (Users_Id, Banned, Role) values ('10', 'No', 'driver')
20 insert into Users (Users_Id, Banned, Role) values ('11', 'No', 'driver')
21 insert into Users (Users_Id, Banned, Role) values ('12', 'No', 'driver')
22 insert into Users (Users_Id, Banned, Role) values ('13', 'No', 'driver')

The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘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|
+----+-----------+-----------+---------+--------------------+----------+

The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘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 |
+----------+--------+--------+

Write a SQL query to find the cancellation rate of requests made by unbanned users between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.

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

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

158ms
1 # Write your MySQL query statement below
2 select request_at Day, round(sum(if(t.Status like 'cancelled%',1,0))/ count(t.Status),2) as 'Cancellation Rate'
3 from trips t
4 join users u
5 on t.client_id = u.users_id
6 and u.banned = 'No'
7 where request_at BETWEEN '2013-10-01' AND '2013-10-03'
8 group by request_at;

160ms

 1 select
 2     R1.Request_at as `Day`,
 3         round(sum(case when R1.Status !='completed' -- in ('cancelled_by_driver','cancelled_by_client') 
 4         then 1
 5         else 0
 6     end)/count(1),2)
 7          as `Cancellation Rate`
 8 from
 9     Trips R1 
10     left join Users R2 on R1.Client_Id = R2.Users_Id
11     left join Users R3 on R1.Driver_Id = R3.Users_Id 
12 where
13             R1.Request_at between '2013-10-01' and '2013-10-03'
14     and R2.Banned = 'No'
15     and R3.Banned = 'No'
16 group by R1.Request_at
17 order by R1.Request_at

162ms

 1 # Write your MySQL query statement below
 2 select
 3 a.request_at as Day,
 4 round(sum(case when status like 'cancelled_by_%'then 1 else 0 end)/count(id),2) as 'Cancellation Rate'
 5 from
 6 trips a
 7 left join
 8 users b
 9 on a.client_id=b.users_id 
10 where b.role='client' and b.banned='No' and a.request_at between '2013-10-01' and '2013-10-03'
11 group by 1
12 order by 1

164ms

1 # Write your MySQL query statement below
2 SELECT T.Request_at as Day, ROUND(SUM(IF(T.Status like "cancelled%", 1, 0))/COUNT(T.Status), 2) as "Cancellation Rate"
3 From Trips as T
4 WHERE (T.Client_Id IN (SELECT Users_Id as aUsers From Users WHERE (Banned like "No") AND (Role like "client"))) 
5         AND (T.Driver_Id IN (SELECT Users_Id as aDrivers From Users WHERE (Banned like "No") AND (Role like "driver")))
6         AND (Request_at BETWEEN "2013-10-01" AND "2013-10-03")
7 GROUP BY Day

 

posted @ 2019-01-06 11:21  为敢技术  阅读(441)  评论(0编辑  收藏  举报