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

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

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

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. 【Linux】linux设备驱动归纳总结

    前言: (总结已经基本写完,这段时间我会从新排版和修正.错误总会有的,望能指正!) 前段时间学习了嵌入式驱动,趁着没开始找工作,这段时间我会每天抽出时间来复习. 我的总结是根据学习时的笔记(李杨老师授 ...

  2. Windows下的开发辅助神器——Chocolate Package Manager

    Windows下的开发辅助神器——Chocolate Package Manager:https://juejin.im/post/5c6cb3acf265da2dc4537235 Windows上的 ...

  3. 第四周课程总结&实验报告(二)

    Java实验报告(二) 实验二 Java简单类与对象 一. 实验目的 (1) 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造 ...

  4. Sigma (化简)牛客多校第一场 -- Integration

    思路: 可以裂项化简,类似找规律,可以两项.三项代进去试试看. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <c ...

  5. 实现远程线程DLL注入

    ### 32位:远程线程注入 远程线程注入是最常用的一种注入技术,该技术利用的核心API是 `CreateRemoteThread()` 这个API可以运行远程线程,其次通过创建的线程调用 `Load ...

  6. Codeforces 1229C. Konrad and Company Evaluation

    传送门 首先考虑如何算出答案,考虑枚举中间那个点,显然每个点作为中间的点的次数为入度乘出度 所以答案就是每个点的入度乘出度之和 然后每个点开一个 $vector$ 维护从它出去的点数,每次修改的时候直 ...

  7. vue中添加与删除,关键字搜索

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. feign 多参数问题

    参考: https://stackoverflow.com/questions/43604734/springboot-feignclient-method-has-too-many-paramter ...

  9. java在遍历列表的时候删除列表中某个元素

    在遍历list的时候需要删除其中的某些元素,不要用foreach遍历,需要用Iterator. List<String> list = new ArrayList<String> ...

  10. python多线程之_thread

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...