【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 ...
随机推荐
- 【Linux开发】编写属于你的第一个Linux内核模块
曾经多少次想要在内核游荡?曾经多少次茫然不知方向?你不要再对着它迷惘,让我们指引你走向前方-- 内核编程常常看起来像是黑魔法,而在亚瑟 C 克拉克的眼中,它八成就是了.Linux内核和它的用户空间是大 ...
- selenium—显示等待中期望的场景语句
① alert_is_present() 判断页面是否出现alert框 wait = WebDriverWait(driver,10) alert = wait.until(EC.alert_is_p ...
- 【图论好题】ABC #142 Task F Pure
题目大意 给定一个 $N$ 个点 $M$ 条边的有向图 $G$,无重边.自环.找出图 $G$ 的一个导出子图(induced subgraph) $G'$,且 $G'$ 中的每个点的入度和出度都是 1 ...
- Eclipse编写代码时代码自动补全 + 防止按空格自动补全
都知道Eclipse中的自动补全代码是一个非常好用的工具 如下: 1.Windows——>Preferences——>Java–>Editor–>点击Content Asist ...
- CF387B 【George and Round】
暴力还真的出奇迹了这题窝将读入的两个数组都先排个序,然后再枚举一遍就过了: 目前题解最短的代码QwQ.这里是代码 #include<bits/stdc++.h>using namespac ...
- 在Qt5使用中文(vs环境)
如果是使用mingw版本的Qt create, 也就是使用GCC编译器应该没那么多事吧. 不过我还是用惯了VS呢. 好了,废话不多说,开始总结vs下乱码的解决方案. vs2003 把源码存成 utf- ...
- Postman之获得登录的token,并设置为全局变量
1.调通登录接口(可以参考上篇博客) 网址:Postman之简单使用 2.粘贴以下代码到Tests中 //把json字符串转化为对象 var data=JSON.parse(responseBody) ...
- 删除链表中重复的结点——牛客剑指offer
题目描述: 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理 ...
- 从尾到头打印列表——牛客剑指offer
题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 解题思路 思路1: 顺序遍历链表,取出每个结点的数据,插入list中. 由于要求list倒序存储链表中的数据,而我们是顺序取 ...
- 110、通过案例学习Secret (Swarm17)
参考https://www.cnblogs.com/CloudMan6/p/8098761.html 在下面的例子中,我们会部署一个 WordPress 应用,WordPress 是流行的开源博客 ...