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. HDU-4219-Randomization?

    题目描述 给定一棵\(n\)个节点的树,每条边的权值为\([0,L]\)之间的随机整数,求这棵树两点之间最长距离不超过\(S\)的概率. Input 第一行三个整数\(n,L,S\) 接下来n-1行, ...

  2. [BZOJ 1013] [JSOI2008]球形空间产生器

    [BZOJ 1013] [JSOI2008]球形空间产生器 题面 给出一个n维球体上的n+1个点,求球心坐标 分析 设球心坐标为\((x_1,x_2,\dots x_n)\),由于一个球体上的所有点到 ...

  3. python-day13(正式学习)

    闭包函数 闭包 闭包:闭是封闭(函数内部函数),包是包含(该内部函数对外部作用域而非全局作用域的变量的引用).闭包指的是:函数内部函数对外部作用域而非全局作用域的引用. 额...这里提示一下闭包!=自 ...

  4. luogu P4383 [九省联考2018]林克卡特树lct

    传送门 题目操作有点奇怪,不过可以发现这就是把树先变成\(k+1\)个连通块,然后每个连通块选一条路径(本题中一个点也是一条路径),然后依次接起来.所以实际上要求的是选出\(k+1\)条点不相交的路径 ...

  5. Linux crontab计划任务

    1.cron计划任务的描述        cron计划任务允许用户根据“时间表”自动周期的完成任务某些任务.        cron是一种system V服务,需要开启该服务才能使用.        ...

  6. SpringBoot01——Framework Introduced and Helloworld

    1.介绍 SpringBoot主要解决的是在微服务的架构下简化配置(有快速配置).前后端分离.快速开发 优点: l 提供了快速启动入门 l 开箱即用.提供默认配置 l 内嵌容器化web项目 l 没有冗 ...

  7. Codeforces Round #606 Div. 2 比赛总结

    比赛情况 bq. A题 Wrong Answer on test 2 , E题sb题没切.bqbqbq. 比赛总结 bq. 那就直接上题解吧!^-^ A 数位dp,分类讨论,注意细节. Talk is ...

  8. Error: unable to perform an operation on node 'rabbit@DESKTOP-6JT7D2H'. Please see diagnostics information and suggestions below.

    https://blog.csdn.net/qq_32814555/article/details/79494533

  9. ng-model 和ng-bind的区别

    也就是说 ng-model是绑定html输入的值-->到控制器的变量,输入值变了,控制器对应的变量message的值页变了,这样,在其他地方就可以使用这个变化后的值 ng-bind 是绑定控制器 ...

  10. Codeforces Round #568 (Div. 2) A.Ropewalkers

    链接: https://codeforces.com/contest/1185/problem/A 题意: Polycarp decided to relax on his weekend and v ...