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的更多相关文章

  1. [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 ...

  2. leetcode database题目

    LeetCode有10道SQL的题目,最近学习SQL语言,顺便刷题强化一下, 说实话刷完SQL学习指南这本书,不是很难,上面的例子 跟语法规则我都能理解透, 实际中来做一些比较难的业务逻辑题,却一下子 ...

  3. 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 ...

  4. leetcode - database - 177. Nth Highest Salary (Oracle)

    题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 1.先按照工资从高到低排序 ...

  5. 【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 ...

  6. LeetCode Database: Delete Duplicate Emails

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

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

  8. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  9. LeetCode Database题解

    175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...

  10. [leetcode] database解题记录

    175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p le ...

随机推荐

  1. Elasticsearch入门教程(二):Elasticsearch核心概念

    原文:Elasticsearch入门教程(二):Elasticsearch核心概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:ht ...

  2. c# 模拟post登录

    使用模拟登录大致可以分为两步 一.post登录获取cookis public CookieContainer GetCookie(string url,string account,string pa ...

  3. fullpage实现(-)

    在线demo还没弄好,地址先给出来

  4. js包装类型的装箱拆箱

    https://www.jb51.net/article/155820.htm https://juejin.im/post/5cbaf130518825325050fb0a https://juej ...

  5. MySQL第三讲 一一一一 视图、触发器、函数、存储过程

    1. 视图 1.1 视图前戏 我们之前讲有,临时表的概念. 现在我们创建一个临时表:select * from (select * from tb1 where id between 10 and 1 ...

  6. Solr IK分词器配置

    下载地址:https://search.maven.org/search?q=com.github.magese 分词器配置: 参考:https://www.cnblogs.com/mengjinlu ...

  7. mariadb增删改查

    数据库用户的操作 登录前需先启动3306端口. 首次启动需初始化数据库 mysql_secure_installation 增/改: 创建用户及赋予用户指定权限 grant 权限(分为create[创 ...

  8. PAT Basic 1091 N-自守数 (15 分)

    如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3,而 2 的末尾两位正好是 9,所以 9 是一个 3-自守数. 本题就请你编写程序判断一个给定的 ...

  9. Spring注解配置、Spring aop、整合Junit——Spring学习 day2

    注解配置: 1.为主配置文件引入新的命名空间(约束) preference中引入文件 2.开启使用注解代理配置文件 <?xml version="1.0" encoding= ...

  10. [易学易懂系列|rustlang语言|零基础|快速入门|(3)|所有权Ownership]

    今天我们来讲讲rust最难,也是最重要的概念: Ownership,Borrowing,Lifetimes 首先我们来看看:ownership(所有权) 我们来看看下面的代码: let a = [1, ...