[LeetCode]-DataBase-Trips and Users
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 clients 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 |
+------------+-------------------+
需求:查询由未绑定的客户端发起的已经取消的订单的占比
create table Trips(
Id tinyint unsigned auto_increment primary key,
Client_Id tinyint unsigned ,
Driver_Id tinyint unsigned ,
City_Id tinyint unsigned,
Status enum('completed', 'cancelled_by_driver', 'cancelled_by_client'),
Request_at date,
foreign key(Client_Id) references Users(Users_Id),
foreign key(Driver_Id) references Users(Users_Id)
)ENGINE=MyISAM;
create table Users(
Users_Id tinyint unsigned auto_increment primary key,
Banned varchar(10),
Role enum('client', 'driver', 'partner')
)ENGINE=MyISAM;
-- 插入数据
INSERT Trips(Id,Client_Id,Driver_Id,City_Id,Status,Request_at)
VALUES(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')
-- Write a SQL query to find the cancellation rate of requests made by unbanned clients
-- between Oct 1, 2013 and Oct 3, 2013.
-- SQL
SELECT a.dt AS 'Day',IF(b.cnt/a.total IS NULL,0.00,ROUND(b.cnt/a.total,2)) AS 'Cancellation Rate'
FROM (
SELECT t1.Request_at AS dt,COUNT(*) AS total
FROM Trips t1 LEFT JOIN Users t2 ON t2.Users_Id=t1.Client_Id
WHERE t2.Role='client' AND t2.Banned='No' AND t1.Request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY Request_at
) a LEFT JOIN(
SELECT t1.Request_at AS dt,COUNT(*) AS cnt
FROM Trips t1
LEFT JOIN Users t2 ON t2.Users_Id=t1.Client_Id
WHERE t2.Role='client' AND t2.Banned='No' AND t1.Status LIKE 'cancelled%'
AND t1.Request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY t1.Request_at
) b ON a.dt=b.dt
[LeetCode]-DataBase-Trips and Users的更多相关文章
- [LeetCode] 262. Trips and Users 旅行和用户
The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are b ...
- leetcode database题目
LeetCode有10道SQL的题目,最近学习SQL语言,顺便刷题强化一下, 说实话刷完SQL学习指南这本书,不是很难,上面的例子 跟语法规则我都能理解透, 实际中来做一些比较难的业务逻辑题,却一下子 ...
- leetcode——262. Trips and Users
The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are b ...
- leetcode - database - 177. Nth Highest Salary (Oracle)
题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 1.先按照工资从高到低排序 ...
- 【leetcode】Trips and Users
The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are b ...
- LeetCode Database: Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- LeetCode Database: Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- LeetCode Database: Consecutive Numbers
Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...
- LeetCode Database题解
175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...
- [leetcode] database解题记录
175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p le ...
随机推荐
- python pycharm 注册码
D87IQPUU3Q-eyJsaWNlbnNlSWQiOiJEODdJUVBVVTNRIiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWduZWVOYW1lIjoiI ...
- google浏览器切换成中文
新浪下载地址:http://down.tech.sina.com.cn/content/40975.html 默认字体好像是西班牙语 1.浏览器地址chrome://settings/language ...
- [转载]排序:长度为n的数组乱序存放着0至n-1. 现在只能进行0与其他数的swap
长度为n的数组乱序存放着0至n-1. 现在只能进行0与其他数的swap 请设计并实现排序. google笔试小题.题目来源:http://wenku.baidu.com/view/5aa818dda5 ...
- 2019-11-29-dotnet-core-输出调试信息到-DebugView-软件
title author date CreateTime categories dotnet core 输出调试信息到 DebugView 软件 lindexi 2019-11-29 10:14:3 ...
- ftp建立虚拟用户实现文件上传和下载
环境 centos7 1.开启vsftpd服务 2.检查vsftpd服务是否开启 3.添加虚拟用户口令文件 vi etc/vsftpd/vuser.txt 4.生成虚拟用户口令认证文件 如果没有db_ ...
- Linux学习--第八天--acl、SetUID、SetGID、chattr、lsattr、sudo
acl权限 文件只能有一个所属组 acl就是不管用户什么组了,直接针对某个文件给他特定权限. acl需要所在分区文件系统的支持. df -h #查看分区 dumpe2fs -h /dev/sda3 # ...
- IIS下设置跨域访问问题--Access-Control-Allow-Origin 站点跨域请求的问题
背景: 最近 开发中遇到新需求,把公司的OA系统迁移一套到小程序上面去 有些功能的信息是在小程序 查看 但是文件是在pc端上传的 例如:领导在外出办公 使用小程序查看xxxx.pdf文件 这个时候就 ...
- 跨域 (3) window.name
window对象有一个name属性,该属性有一个特征:即在一个窗口的生命周期内,窗口载入的所有的页面都是共享一个window.name的,每一个页面对window.name都有读写的权限,window ...
- ZROI 19.08.02 杂题选讲
给出\(n\)个数,用最少的\(2^k\)或\(-2^{k}\),使得能拼出所有数,输出方案.\(n,|a_i|\leq 10^5\). 显然一个绝对值最多选一次.这个性质非常强. 如果所有都是偶数, ...
- Python 3标准库第四章
第四章日期和时间----------------- 不同于int.float和str,Python没有包含对应日期和时间的原生类型,不过提供了3个相应的模块,可以采用多种表示来管理日期和时间值. ...