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 theUsers 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 betweenOct 1, 2013 andOct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded totwo decimal places.

+------------+-------------------+
|     Day    | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 |       0.33        |
| 2013-10-02 |       0.00        |
| 2013-10-03 |       0.50        |
+------------+-------------------+

要求找出每天unbaned clients的取消记录的概率。

这道题感觉思路还是比较简单的,不知道为什么会评定为hard,下面说下思路:

首先在trip中找出日期区间内符合条件的记录,sql语句如下:

select * from Trips
where client_id not in (select users_id from Users where banned = "yes" and role = "client")
and request_at >= "2013-10-01" and request_at <= "2013-10-03"

然后对于这些记录根据日期(request_at列)进行分组,找出每组符合status!='completed'条件的元组的个数作为分子,分组内记录个数总数作为分母,分子除以分母得出结果。

本来分子我是这么写的:count(status!='completed') 结果得不到正确的结果,貌似这种写法是错误的;

后来改成了sum( case when status='completed' then 0 else 1 end)的写法,运行通过了;

注意得出来的比率要用round函数取小数点后两位,完整的sql语句如下:

select request_at,round(sum(case when status="completed" then 0 else 1 end)/count(*),2) from
(select * from Trips
where client_id not in (select users_id from Users where banned = "yes" and role = "client") and request_at >= "2013-10-01" and request_at <= "2013-10-03") t
group by request_at order by request_at asc

LeetCode_OJ【262】Trips and Users相关推荐

  1. 【262】pscp命令 实现windows与linux互传文件

    首先将pscp.exe文件放在某个文件夹中 新建*.bat文件 w-wx.bat代码 @echo off pscp.exe -pw l*****h D:\Windows-Linux\Data\* oc ...

  2. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  3. 【Linux】一步一步学Linux——indent命令(262)

    00. 目录 文章目录 00. 目录 01. 命令概述 02. 命令格式 03. 常用选项 04. 参考示例 05. 附录 01. 命令概述 indent命令可识别C语言代码文件,并加以格式化,以方便 ...

  4. 【SQL】LeetCode-Trips and Users

    LeetCode 262:Trips and Users [Description] Write a SQL query to find the cancellation rate of reques ...

  5. Leetcode SQL 刷题 Mysql【2】

    [11]595. 大的国家 select name,population,area from World where area > 3000000 or population > 2500 ...

  6. 【ES6】JS的Set和Map数据结构

    [ES6]JS的Set和Map数据结构 一.Set 1.基本用法 2.4种操作方法 3.4种遍历方法 4.Set的应用 1)Set转化为数组 2)去除数组的重复元素 3)实现并集(Union).交集( ...

  7. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  8. oracle11g知乎,【AAAI】AAAI2020录用论文汇总(二)

    因为AAAI的接受论文官方还没有放出,并且放的也是出奇的慢,本文汇总了23日在arxiv上挂出来的AAAI2020文章,供大家挑选感兴趣的文章下载.第一部分可以看 忆臻:[AAAI]AAAI2020录 ...

  9. 【POI】对于POI无法处理超大xls等文件,官方解决方法【已解决】【多线程提升速率待定】...

    本次使用POI处理xlsx文件,莫名的遇到了一个无法逾越的问题. 总共71个xlsx文件,单个文件最大达到50M以上,71个xls文件摆在那里就有3-4G的大小. 在起始处理的时候,发现原本适用于正常 ...

最新文章

  1. angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器
  2. HDU 1198 Farm Irrigation
  3. 数据库-优化-pt-kill-授权-数据
  4. json数据交互---SpringMVC学习笔记(十二)
  5. Python之日志处理(logging模块)详解
  6. 开启爬虫之路,从零开始...
  7. AI版“大家来找茬”上线,究竟谁是真人,谁是GAN生成的假脸?
  8. [bug解决] TensorFlow安装错误:ERROR After October 2020 you may experience errors when installing
  9. HDU 2087 剪花布条 KMP入门
  10. android环境混合app开发,cordova混合App开发:Cordova+Vue实现Android (环境搭建)
  11. php date 函数用法,PHP日期时间函数date()使用方法
  12. STM32F7xx基于HAL库的USB_CDC接收数据的函数调用
  13. excel学习1:合并两个单元格,并把内容用符号隔开。
  14. html banner 居中,关于CSS banner图响应式居中显示的方法
  15. 转盘抽奖角度计算 前端
  16. Gateway NV47H18C BIOS 密码清除
  17. ip.php是什么意思,有人频繁试探云主机的 ip_js. PHP 是什么操作?
  18. 3_1 操作系统定义、分类及功能【包含linux操作系统基础知识】
  19. 北京大学计算机视觉导师,北京大学信息科学技术学院林宙辰研究生导师介绍
  20. 软件架构设计原则-里氏替换原则

热门文章

  1. 收集的SQL Server性能相关资料
  2. 国产紫光FPGA实现DDS信号发生器
  3. 全球及中国航空机电开关行业重点领域需求及未来发展展望报告2022-2028年
  4. 计算机桌面怎么全屏显示,电脑显示器如何设置全屏 把电脑屏幕调成满屏的方法有哪些...
  5. 显卡出问题,花屏,显示蓝条了,分辨率800*600,想办法终于把问题定位了
  6. Remove specific element by editing the array
  7. 抄代码对自己编程提高有用吗?
  8. 深夜,我常逛的几个网站。
  9. java智能卡操作系统_智能卡操作系统(COS),什么是智能卡操作系统(COS)
  10. 小博无线认证无法连接服务器,路由器wan口认证失败