博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】Trips and Users
阅读量:6957 次
发布时间:2019-06-27

本文共 4581 字,大约阅读时间需要 15 分钟。

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;

 

转载于:https://www.cnblogs.com/seyjs/p/5130104.html

你可能感兴趣的文章
【系统架构师修炼之道】(4):绪论——Zachman 框架
查看>>
Foxify v0.10.7 发布,基于 TypeScript 的 Node 框架
查看>>
Python数据结构——双端队列
查看>>
GitHub 项目推荐:用深度学习让你的照片变得美丽 ...
查看>>
另类文件加密 图片当密码给文本加密
查看>>
MySQL数据库如何解决大数据量存储问题
查看>>
CENTOS6.5 yum配置
查看>>
《自顶向下网络设计(第3版)》——1.6 复习题
查看>>
【转】微信小程序给程序员带来的可能是一个赚钱的机遇
查看>>
《Programming Ruby中文版:第2版》终于正式出版了
查看>>
使用Observium来监控你的网络和服务器
查看>>
蚂蚁区块链团队资讯简报20170514
查看>>
线性空间(向量空间)
查看>>
多媒体之录音
查看>>
mysql 分区类型详解
查看>>
ORACLE同义词总结
查看>>
ios字体设置
查看>>
【SICP练习】51 练习2.19
查看>>
solveCrossprod求 X'X的逆, ( X' == t(X), 即X的行列变换矩阵 )
查看>>
PostgreSQL column cryptographic use pgcrypto extension and optional openssl lib
查看>>