【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;