【leetcode】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 |
+------------+-------------------+ 解题思路:本题属于hard级别,但我个人觉得其难度不大。 题目的意思是要求出2013-10-01到2013-10-03这三天内,每天非Banned用户取消订单的比率,取消订单包括乘客取消和司机取消。 1.取出所有满足条件的记录,这次加了一个按照日期排序,给后续统计使用
select * from Trips a, Users b where a.Client_Id = b.Users_Id and b.Role = 'client' and b.Banned = 'No' and Request_at between '2013-10-01' and '2013-10-03' order by Request_at;
2.依次遍历所有符合条件的记录,引入中间遍历@day(当前记录的日期),@lastday(上一条记录的日期),@snum(完成的记录数),@cnum(被取消的记录数),@totalnum(记录总数)。如果@day和@lastday相等,表示日期在同一天,根据订单状态分别给@snum或@cnum加1,同时给@totalnum加1;
如果不相等,表示日期变换了,如果订单状态成功让@snum = 1,@cnum = 0,如果失败让@snum = 0,@cnum = 1,同时给@totalnum = 1。这也就是为什么在第一步中要按日期排序的原因了,遍历完成后,同一日期的最后一条记录中的@snum,@cnum,@totalnum就是这个日期对应的各状态的数量。
select Request_at,
@lastday:=@day,
case when @day = '' then @day:=Request_at when @day != Request_at then (@day:=Request_at) else @day:=Request_at end,
@snum := if(@lastday = Request_at , if(Status='completed',@snum+1,@snum), if(Status='completed',@snum:=1,@sum:=0)) as success ,
@cnum := if(@lastday = Request_at , if(Status!='completed',@cnum+1,@cnum), if(Status!='completed',@cnum:=1,@csum:=0)) as fail ,
@totalnum := if(@lastday = Request_at , @totalnum+1, @totalnum:=1) as total
from (select Request_at,Status,Client_Id from Trips order by Request_at) a, Users b ,(select @snum:=0,@cnum:=0,@day:='',@lastday:='',@totalnum:=0) c where a.Client_Id = b.Users_Id and b.Role = 'client' and b.Banned = 'No' order by Request_at,total desc;
3.用@cnum除以@totalnum求出商即可
select Request_at as Day,round(fail/total,2) as 'Cancellation Rate' from
(
select Request_at,
@lastday:=@day,
case when @day = '' then @day:=Request_at when @day != Request_at then (@day:=Request_at) else @day:=Request_at end,
@snum := if(@lastday = Request_at , if(Status='completed',@snum+1,@snum), if(Status='completed',@snum:=1,@sum:=0)) as success ,
@cnum := if(@lastday = Request_at , if(Status!='completed',@cnum+1,@cnum), if(Status!='completed',@cnum:=1,@csum:=0)) as fail ,
@totalnum := if(@lastday = Request_at , @totalnum+1, @totalnum:=1) as total
from (select Request_at,Status,Client_Id from Trips order by Request_at) a, Users b ,(select @snum:=0,@cnum:=0,@day:='',@lastday:='',@totalnum:=0) c where a.Client_Id = b.Users_Id and b.Role = 'client' and b.Banned = 'No' and Request_at between '2013-10-01' and '2013-10-03' order by Request_at ,total desc
) d group by Request_at;
【leetcode】Trips and Users的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- [c++] WINAPI
int WINAPI WINMain 中,WINAPI含义 在windef.h头文件中有如下定义: #define WINAPI __stdcall #define APIENTRY WINAPI 函 ...
- Linux下面查看网卡的信息
查看linux下面网卡的速度信息 Study From 百度知道 (懒得翻墙) 1. centos机器 安装的比较全(个人比较懒 没有使用core最小化安装, 避免出问题麻烦 公司网络太垃圾) 使用 ...
- Ubuntu 系统安装 Docker
安装 Docker CE 有多种方法,下面是最简单的通过Docker仓库的安装方法,其他方法参见官方文档. 设置仓库 刷新软件包 sudo apt-get update 安装必要的软件包 sudo a ...
- MYSQL中的UNION和UNION ALL
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- Python 入门 之 包
Python 入门 之 包 1.包 (1)什么是包? 文件夹下具有_ init.py _的文件夹就是一个包 (2)包的作用: 管理模块(文件化) (3)包的导入: 导入: 启动文件要和包文件是同级 绝 ...
- Python简单主机批量管理工具
一.程序介绍 需求: 简单主机批量管理工具 需求: 1.主机分组 2.主机信息使用配置文件 3.可批量执行命令.发送文件,结果实时返回 4.主机用户名密码.端口可以不同 5.执行远程命令使用param ...
- 微信小程序使用页面栈改变上一页面的数据
微信小程序中如果从一个页面中进入下一个页面,如果下个页面的数据有删除或者增加再返回上一个页面的时候,就会导致页面不刷新(数据加载函数在onload中),从而造成数据不一致的情况.其实在微信小程序中是可 ...
- float详解
先上一个简单示例,了解一下float的使用 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Echarts ajax异步
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- springboot(十六)-swagger2
SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API ...