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. webdriervAPI(窗口截图)

    from  selenium  import  webdriver driver  =  webdriver.Chorme() driver.get("http://www.baidu.co ...

  2. 题目---汉诺塔及AI代码及八皇后

    2019春第十一周作业 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering ...

  3. Elasticsearch-布尔类型

    boolean类型用于存储文档中的true/false.例如:专辑类型中需要添加一个字段表示是否可以下载,如下 curl -XPUT 'localhost:9200/music/album/4' -d ...

  4. Linux动态链接之GOT与PLT

    转载于:http://www.cnblogs.com/xingyun/archive/2011/12/10/2283149.html   我们知道函数名就是一个内存地址,这个地址指向函数的入口.调用函 ...

  5. 小记---------spring框架之IOC理解

    Spring是一个开源框架,是一个轻量级的Java开发框架. Spring的核心是控制发转(IOC)和面向切面(AOP)   控制发转(IOC):指的是 对象的创建权反转(交给)给 Spring. 作 ...

  6. C++多线程基础学习笔记(七)

    一.std::async和std::future的用法 std::async是一个函数模板,std::future是一个类模板 #include <iostream> #include & ...

  7. <<C++ Primer>> 第 6 章 函数

    术语表 第 6 章 函数 二义性调用(ambiguous call): 是一种编译时发生的错误,造成二义性调用的原因时在函数匹配时两个或多个函数提供的匹配一样好,编译器找不到唯一的最佳匹配.    实 ...

  8. 小白学习django第四站-关联数据库

    使用mysql连接django首先要配置好相关环境 首先在setting.py配置数据库信息(需要现在mysql中创建一个数据库) 在setting.py那个目录的__init__.py文件中写入 之 ...

  9. 解决Sublime Text3中文符号以及中文显示乱码问题

    今天安装了sublime Text3,发现中文符号显示是乱码,刚开始以为是编码问题,经过各种尝试,终于找到了解决办法.解决方法如下: 一.安装包管理器 使用Ctrl+~快捷键或者通过View-> ...

  10. MySQL中文正常而mybatis查询出现乱码的解决方案

    解决方案是在spring-mvc.xml文件中,加入 <mvc:annotation-driven> <mvc:message-converters> <bean cla ...