[LeetCode] 262. 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        |
+------------+-------------------+

Trips表里有一些Id, 状态,请求时间。Users表里有顾客和司机信息, 还有该顾客和司机有没有被Ban的信息。要返回一个结果看某个时间段内由没有被ban的顾客提出的取消率是多少。其实题目没有说清楚顾客到底包不包括司机,其实是包括的,由司机提出的取消请求也应计算进去,用Case When ... Then ... Else ... End关键字来做,用cancelled%来表示开头是cancelled的所有项,这样就包括了driver和client,然后分母是所有项,限制条件里限定了时间段,然后是没有被ban的,结果需要保留两位小数,所以用Round关键字给定参数2。

解法1:

SELECT t.Request_at Day, ROUND(SUM(CASE WHEN t.Status LIKE ‘cancelled%‘ THEN 1 ELSE 0 END)/COUNT(*), 2) ‘Cancellation Rate‘
FROM Trips t JOIN Users u ON t.Client_Id = u.Users_Id AND u.Banned = ‘No‘
WHERE t.Request_at BETWEEN ‘2013-10-01‘ AND ‘2013-10-03‘ GROUP BY t.Request_at;  

解法2:

SELECT Request_at Day, ROUND(COUNT(IF(Status != ‘completed‘, TRUE, NULL)) / COUNT(*), 2) ‘Cancellation Rate‘
FROM Trips WHERE (Request_at BETWEEN ‘2013-10-01‘ AND ‘2013-10-03‘) AND Client_Id IN
(SELECT Users_Id FROM Users WHERE Banned = ‘No‘) GROUP BY Request_at;

  

原文地址:https://www.cnblogs.com/lightwindy/p/9532799.html

时间: 2024-08-30 14:09:20

[LeetCode] 262. Trips and Users 旅行和用户的相关文章

[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’). +----+----

【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’). +----+----

262. Trips and Users (Hard)

Source: https://leetcode.com/problems/trips-and-users/#/descriptionDescription: 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

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

SQL数据库—<…> 常用系统存储过程大全 --摘录网络

1 -- 来源于网络 2 3 -- 更详细的介结参考联机帮助文档 4 5 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. 6 7 xp_fixeddrives --*查询各磁盘/分区可用空间 8 9 xp_loginconfig --*报告SQL Server 实例在Windows 上运行时的登录安全配置 10 11 xp_logininfo --*返回有关Windows 认证登录的信息. 12 13 xp_msver --*返回有关Microsoft SQL Server

使用触摸检测滑动(左,右,上或下)

触摸式滑动是指在特定方向上快速移动手指穿过触摸表面的行为.onswipeJavaScript中目前没有" "事件,这意味着我们可以使用可用的触摸事件来实现一个事件,并且只需在刷卡时定义,即"刷卡". 让我们首先定义何时应该将触摸表面上的移动视为滑动.有在玩两个变量这里-在距离旅行了用户对x或y轴的手指touchstart来 touchend,而且,它花时间.基于这两个因素,我们可以决定该操作是否符合滑动和方向. 有了这个,让我们把想法付诸行动,看看如何检测右键(从左

将最小的OWIN身份验证添加到现有的ASP.NET MVC应用程序

https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application 将最小的OWIN身份验证添加到现有的ASP.NET MVC应用程序 2015年4月29日•来自毛伊岛,HI•    40条评论 从ASP.NET 4开始,ASP.NET提供了一个相当有用的身份系统.如果您创建一个新项目并选择一个MVC项目并选

397.整数替换

题目描述: 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n. 2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n. 问: n 变为 1 所需的最小替换次数是多少? 可见,该题的难点在于n是奇数时,应该是n+1还是n-1. 解法一:递归 当n==1时,return 0; 将int型的值传给m时,需要将m的类型申明为long long型.因为如果int 型的n为2147483647时,m=(n+1)/2的过程中n+1就会溢出. int, lon