SQL架构

Table: Drivers

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| driver_id   | int     |
| join_date   | date    |
+-------------+---------+
driver_id is the primary key for this table.
Each row of this table contains the driver's ID and the date they joined the Hopper company.

Table: Rides

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| ride_id      | int     |
| user_id      | int     |
| requested_at | date    |
+--------------+---------+
ride_id is the primary key for this table.
Each row of this table contains the ID of a ride, the user's ID that requested it, and the day they requested it.
There may be some ride requests in this table that were not accepted.

Table: AcceptedRides

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| ride_id       | int     |
| driver_id     | int     |
| ride_distance | int     |
| ride_duration | int     |
+---------------+---------+
ride_id is the primary key for this table.
Each row of this table contains some information about an accepted ride.
It is guaranteed that each accepted ride exists in the Rides table.

Write an SQL query to report the percentage of working drivers (working_percentage) for each month of 2020 where:

Note that if the number of available drivers during a month is zero, we consider the working_percentage to be 0.

Return the result table ordered by month in ascending order, where month is the month's number (January is 1, February is 2, etc.). Round working_percentage to the nearest 2 decimal places.

The query result format is in the following example.

Example 1:

Input:
Drivers table:
+-----------+------------+
| driver_id | join_date  |
+-----------+------------+
| 10        | 2019-12-10 |
| 8         | 2020-1-13  |
| 5         | 2020-2-16  |
| 7         | 2020-3-8   |
| 4         | 2020-5-17  |
| 1         | 2020-10-24 |
| 6         | 2021-1-5   |
+-----------+------------+
Rides table:
+---------+---------+--------------+
| ride_id | user_id | requested_at |
+---------+---------+--------------+
| 6       | 75      | 2019-12-9    |
| 1       | 54      | 2020-2-9     |
| 10      | 63      | 2020-3-4     |
| 19      | 39      | 2020-4-6     |
| 3       | 41      | 2020-6-3     |
| 13      | 52      | 2020-6-22    |
| 7       | 69      | 2020-7-16    |
| 17      | 70      | 2020-8-25    |
| 20      | 81      | 2020-11-2    |
| 5       | 57      | 2020-11-9    |
| 2       | 42      | 2020-12-9    |
| 11      | 68      | 2021-1-11    |
| 15      | 32      | 2021-1-17    |
| 12      | 11      | 2021-1-19    |
| 14      | 18      | 2021-1-27    |
+---------+---------+--------------+
AcceptedRides table:
+---------+-----------+---------------+---------------+
| ride_id | driver_id | ride_distance | ride_duration |
+---------+-----------+---------------+---------------+
| 10      | 10        | 63            | 38            |
| 13      | 10        | 73            | 96            |
| 7       | 8         | 100           | 28            |
| 17      | 7         | 119           | 68            |
| 20      | 1         | 121           | 92            |
| 5       | 7         | 42            | 101           |
| 2       | 4         | 6             | 38            |
| 11      | 8         | 37            | 43            |
| 15      | 8         | 108           | 82            |
| 12      | 8         | 38            | 34            |
| 14      | 1         | 90            | 74            |
+---------+-----------+---------------+---------------+
Output:
+-------+--------------------+
| month | working_percentage |
+-------+--------------------+
| 1     | 0.00               |
| 2     | 0.00               |
| 3     | 25.00              |
| 4     | 0.00               |
| 5     | 0.00               |
| 6     | 20.00              |
| 7     | 20.00              |
| 8     | 20.00              |
| 9     | 0.00               |
| 10    | 0.00               |
| 11    | 33.33              |
| 12    | 16.67              |
+-------+--------------------+
Explanation:
By the end of January --> two active drivers (10, 8) and no accepted rides. The percentage is 0%.
By the end of February --> three active drivers (10, 8, 5) and no accepted rides. The percentage is 0%.
By the end of March --> four active drivers (10, 8, 5, 7) and one accepted ride by driver (10). The percentage is (1 / 4) * 100 = 25%.
By the end of April --> four active drivers (10, 8, 5, 7) and no accepted rides. The percentage is 0%.
By the end of May --> five active drivers (10, 8, 5, 7, 4) and no accepted rides. The percentage is 0%.
By the end of June --> five active drivers (10, 8, 5, 7, 4) and one accepted ride by driver (10). The percentage is (1 / 5) * 100 = 20%.
By the end of July --> five active drivers (10, 8, 5, 7, 4) and one accepted ride by driver (8). The percentage is (1 / 5) * 100 = 20%.
By the end of August --> five active drivers (10, 8, 5, 7, 4) and one accepted ride by driver (7). The percentage is (1 / 5) * 100 = 20%.
By the end of September --> five active drivers (10, 8, 5, 7, 4) and no accepted rides. The percentage is 0%.
By the end of October --> six active drivers (10, 8, 5, 7, 4, 1) and no accepted rides. The percentage is 0%.
By the end of November --> six active drivers (10, 8, 5, 7, 4, 1) and two accepted rides by two different drivers (1, 7). The percentage is (2 / 6) * 100 = 33.33%.
By the end of December --> six active drivers (10, 8, 5, 7, 4, 1) and one accepted ride by driver (4). The percentage is (1 / 6) * 100 = 16.67%.

with recursive t3 as (select '2020-01-01' n union all select date_add(n,interval 1 month) # 得到 (2020年的 1到12月 但是 会多一个 2021年1yue 不要紧后面会滤掉)from t3 where year(n) =2020),t2 as (select month(df) `month` , max(co) mcfrom(select  # 2020 每月 Hopper公司工作的驾驶员数量join_date df,count(driver_id) over(order by join_date rows between unbounded preceding and current row) cofrom  (selectdriver_id,join_datefromDriversunion allselectnull driver_id, n join_datefromt3where  date_format(n,'%Y-%m') not in (selectdate_format(join_date,'%Y-%m')fromDrivers))ss2)s2where year(df) = 2020group by month(df)
), t1 as (# 不是每个月接受的乘车次数 是每个月接受乘车的司机个数 (应去重)********************************selectcount(distinct driver_id) cdi,month(requested_at) `month` from(select a.driver_id,r.requested_atfromAcceptedRides a left join Rides  rusing(ride_id))s1where date_format(requested_at,'%Y') = '2020'group by date_format(requested_at,'%Y-%m'))
select t2.month `month`,round(if( t2.mc = 0,0, ifnull(t1.cdi,0) /t2.mc *100),2) working_percentage
from
t2 left join  t1  using(`month`)  # t2 left join t1 得出题中需求
order by `month`

1645. Hopper Company Queries II相关推荐

  1. SPOJ GSS2 Can you answer these queries II (线段树离线) - xgtao -

    Can you answer these queries II 这是一道线段树的题目,维护历史版本,给出N(<=100000)个数字(-100000<=x<=100000),要求求出 ...

  2. GSS2 - Can you answer these queries II

    GSS2 - Can you answer these queries II 题意: 给你1e51e51e5 的序列,每次询问区间l到rl到rl到r,每个相同的数只算一次的最大子段和. 思路: 乍一眼 ...

  3. Can you answer these queries II

    题意: 给一长度为n的序列,有m组询问,每一组询问给出[l,r]询问区间内的最大去重连续子段和. 解法: 考虑一下简化后的问题:如果题目要求询问查询以$r$为右端点且$l$大于等于给定值的去重连续子段 ...

  4. SP1557 GSS2 - Can you answer these queries II

    一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊-- 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...

  5. SPOJ GSS2 Can you answer these queries II

    很好的一道题,想了很久.首先突破的是,可以找到枚举其中一边,假设是尾部y,然后快速找出满足条件的最大的头部x,连续区间的和 很容易想到借助部分和的思想,如果是从y开始往前面累加,那么就是一个关于y的后 ...

  6. 【数据库系统】数据库系统学习与实践系列文章汇总目录(持续更新中)

    本文属于「数据库系统」系列文章的汇总目录,这一系列着重于「数据库系统知识的学习与实践」.由于文章内容随时可能发生更新变动,欢迎关注和收藏本文以作备忘.需要特别说明的是,为了透彻理解和全面掌握数据库系统 ...

  7. python中csv文件操作_python中操作csv文件

    python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: pri ...

  8. 数据结构---线段树

    线段树 转载请注明出处,谢谢!http://blog.csdn.net/metalseed/article/details/8039326  持续更新中···   一:线段树基本概念 1:概述 线段树 ...

  9. 【转】线段树题目 汇总 讲解(by not only success)

    转载自:http://www.notonlysuccess.com/ 非常喜欢他的代码风格以及简洁的思路,感谢notonlysuccess! PS:他的个人网站好像是上不去了-.- 线段树 很早前写的 ...

最新文章

  1. 缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级
  2. JavaScript 字典类
  3. JDK源码解析之 Java.lang.Float
  4. telnet服务下载 Linux,linux telnet服务安装包
  5. s3c6410 uboot代码分析《二》
  6. android 判断两个整数,【tips】判断两个整数是否是同一个数量级
  7. Spring Boot 全局异常捕获
  8. Linux 安装MySql 5.7.21 操作步骤
  9. 区分const,static,readonly,volatile四个关键字
  10. matlab程序 直线插补,用Matlab实现直线插补计算程序讲解学习
  11. 串口调试工具和串口下载工具的区别
  12. 计算机cad运行缓慢怎样处理,win7系统提高CAD运行速度的方法
  13. mysql中rtrim的用法,MySQL RTRIM()用法及代码示例
  14. 《SysML精粹》学习记录--第八章
  15. 中国软件外包企业的出路
  16. html语言计算圆周长和面积,c# 根据半径计算圆的面积和周长
  17. 计算机专业应该拿的几个证书
  18. 无线网卡抓包小记--我的无线网卡终于能抓包了
  19. Java工程师核心能力_java程序员的核心竞争力是什么?
  20. 李兴华 java pdf_李兴华+Java核心技术精讲 PDF 下载

热门文章

  1. 【遇见CUDA】CUDA算法效率提升关键点概述
  2. 交换机和路由器的区别_路由器与交换机的区别与联系
  3. 华为路由器ws5200虚拟服务器,华为路由器配置dhcp怎么弄?华为路由WS5200设置DHCP服务器方法...
  4. 子元素padding一般不会撑开父元素盒子大小
  5. 大华摄像头使用外网进行访问管理
  6. ognl.NoSuchPropertyException(没有对应属性异常)
  7. 浅析人工智能体系建设
  8. android------之高德地图实现定位和3D地图显示
  9. This must be due to duplicate classes or playing wrongly with class loaders 1
  10. TPLINK AC650双频高增益无线USB网卡 TL-WDN5200H免驱版 Ubuntu16.04 安装