首页 > 其他分享 >1645. Hopper 公司查询 II - 力扣(LeetCode)

1645. Hopper 公司查询 II - 力扣(LeetCode)

时间:2025-01-06 09:02:33浏览次数:3  
标签:1645 INSERT INTO ride Hopper month 力扣 VALUES id

  1. 1645. Hopper 公司查询 II - 力扣(LeetCode)

  2. 目标

    1. 输入

      表 Rides:
      ride_iduser_idrequested_at
      6752019/12/9
      1542020/2/9
      10632020/3/4
      19392020/4/6
      3412020/6/3
      13522020/6/22
      7692020/7/16
      17702020/8/25
      20812020/11/2
      5572020/11/9
      2422020/12/9
      11682021/1/11
      15322021/1/17
      12112021/1/19
      14182021/1/27
      表 Drivers:
      driver_idjoin_date
      102019/12/10
      82020/1/13
      52020/2/16
      72020/3/8
      42020/5/17
      12020/10/24
      62021/1/5
      表 AcceptedRides:
      ride_iddriver_idride_distanceride_duration
      10106338
      13107396
      7810028
      17711968
      20112192
      5742101
      24638
      1183743
      15810882
      1283834
      1419074
    2. 输出

      输出
      monthworking_percentage
      10
      20
      325
      40
      50
      620
      720
      820
      90
      100
      1133.33
      1216.67
  3. 分析

    编写解决方案以报告 2020 年每个月的工作驱动因素 百分比(working_percentage),其中:


    注意:如果一个月内可用驾驶员的数量为零,我们认为 working_percentage 为 0。
    返回按 month 升序 排列的结果表,其中 month 是月份的编号(一月是 1,二月是 2,等等)。将 working_percentage 四舍五入至 小数点后两位。
    表 Rides:表 AcceptedRides:表 Drivers:输出
    ride_iduser_idrequested_atride_iddriver_idride_distanceride_durationdriver_idjoin_datemonthworking_percentage
    6752019/12/910106338102019/12/1010
    1542020/2/91310739682020/1/1320
    10632020/3/4781002852020/2/16325
    19392020/4/61771196872020/3/840
    3412020/6/32011219242020/5/1750
    13522020/6/22574210112020/10/24620
    7692020/7/162463862021/1/5720
    17702020/8/251183743820
    20812020/11/21581088290
    5572020/11/91283834100
    2422020/12/914190741133.33
    11682021/1/111216.67
    15322021/1/17
    12112021/1/19
    14182021/1/27
    递归月份month每月累计驾驶员数量monthactive_drivers每月接受了订单的驾驶员数量monthAR_CNT求百分比monthworking_percentage
    1123110
    2236120
    33471325
    4448140
    55511250
    665121620
    77520720
    88540820
    9951090
    1010650100
    11116901133.33
    121261001216.67
  4. 实现

    CREATE TABLE IF NOT EXISTS Drivers
    (
        driver_id INT,
        join_date DATE
    );
    CREATE TABLE IF NOT EXISTS Rides
    (
        ride_id      INT,
        user_id      INT,
        requested_at DATE
    );
    CREATE TABLE IF NOT EXISTS AcceptedRides
    (
        ride_id       INT,
        driver_id     INT,
        ride_distance INT,
        ride_duration INT
    );
    TRUNCATE TABLE Drivers;
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('10', '2019-12-10');
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('8', '2020-1-13');
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('5', '2020-2-16');
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('7', '2020-3-8');
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('4', '2020-5-17');
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('1', '2020-10-24');
    INSERT INTO Drivers (driver_id, join_date)
    VALUES ('6', '2021-1-5');
    TRUNCATE TABLE Rides;
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('6', '75', '2019-12-9');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('1', '54', '2020-2-9');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('10', '63', '2020-3-4');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('19', '39', '2020-4-6');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('3', '41', '2020-6-3');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('13', '52', '2020-6-22');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('7', '69', '2020-7-16');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('17', '70', '2020-8-25');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('20', '81', '2020-11-2');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('5', '57', '2020-11-9');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('2', '42', '2020-12-9');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('11', '68', '2021-1-11');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('15', '32', '2021-1-17');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('12', '11', '2021-1-19');
    INSERT INTO Rides (ride_id, user_id, requested_at)
    VALUES ('14', '18', '2021-1-27');
    TRUNCATE TABLE AcceptedRides;
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('10', '10', '63', '38');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('13', '10', '73', '96');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('7', '8', '100', '28');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('17', '7', '119', '68');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('20', '1', '121', '92');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('5', '7', '42', '101');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('2', '4', '6', '38');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('11', '8', '37', '43');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('15', '8', '108', '82');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('12', '8', '38', '34');
    INSERT INTO AcceptedRides (ride_id, driver_id, ride_distance, ride_duration)
    VALUES ('14', '1', '90', '74');
    SELECT *
    FROM Rides;
    SELECT *
    FROM AcceptedRides;
    SELECT *
    FROM Drivers;
    # 递归月份
    WITH RECURSIVE T1 (month) AS (SELECT
                                      1
                                  FROM dual
                                  UNION ALL
                                  SELECT
                                      month + 1
                                  FROM T1
                                  WHERE month < 12)
    SELECT *
    FROM T1
    ;
    # 计算驾驶员数量
    WITH RECURSIVE T1 (month) AS (SELECT
                                      1
                                  FROM dual
                                  UNION ALL
                                  SELECT
                                      month + 1
                                  FROM T1
                                  WHERE month < 12)
    SELECT
        month,
        COUNT(IF(d.driver_id IS NOT NULL, 1, NULL)) +
        (SELECT COUNT(IF(driver_id IS NOT NULL, 1, NULL)) FROM Drivers WHERE YEAR(join_date) < 2020) active_drivers
    FROM T1
    LEFT JOIN Drivers D ON T1.month >= MONTH(join_date) AND YEAR(join_date) = 2020
    GROUP BY month
    ORDER BY month
    ;
    # 计算每月有多少驾驶员接受了订单
    WITH RECURSIVE T1 (month) AS (SELECT
                                      1
                                  FROM dual
                                  UNION ALL
                                  SELECT
                                      month + 1
                                  FROM T1
                                  WHERE month < 12)
    SELECT
    month,COUNT(AR.driver_id)AR_CNT
    FROM T1
    LEFT JOIN Rides R ON YEAR(requested_at)=2020 AND month=MONTH(requested_at)
    LEFT JOIN AcceptedRides AR ON R.ride_id = AR.ride_id
    GROUP BY month;
    # 求百分比
    WITH RECURSIVE T1 (month) AS (SELECT
                                      1
                                  FROM dual
                                  UNION ALL
                                  SELECT
                                      month + 1
                                  FROM T1
                                  WHERE month < 12)
    ,T2 AS (
    SELECT month,COUNT(AR.driver_id)AR_CNT
    FROM T1
    LEFT JOIN Rides R ON YEAR(requested_at)=2020 AND month=MONTH(requested_at)
    LEFT JOIN AcceptedRides AR ON R.ride_id = AR.ride_id
    GROUP BY month
     )
     ,T3 AS (
     SELECT
        month,
        COUNT(IF(d.driver_id IS NOT NULL, 1, NULL)) +
        (SELECT COUNT(IF(driver_id IS NOT NULL, 1, NULL)) FROM Drivers WHERE YEAR(join_date) < 2020) active_drivers
    FROM T1
    LEFT JOIN Drivers D ON T1.month >= MONTH(join_date) AND YEAR(join_date) = 2020
    GROUP BY month
    ORDER BY month
      )SELECT T2.month,IF(ROUND (AR_CNT/active_drivers*100,2) IS NOT NULL ,ROUND(AR_CNT/active_drivers*100,2),0)working_percentage
      FROM T2,T3 WHERE T2.month=T3.month
      ORDER BY month

  5. 小结

        递归,多表连接,分组聚合

标签:1645,INSERT,INTO,ride,Hopper,month,力扣,VALUES,id
From: https://blog.csdn.net/2301_78665414/article/details/144955336

相关文章

  • 力扣1. 两数之和
    相信很多人和我一样(本人较......
  • 力扣994题:腐烂的橘子
     评论区有很多bfs板子的解法,我这里没有用队列,介绍一种很直白暴力,容易理解的方法。思路其实很简单,先搜索一遍,计算有橘子的数量,也就是腐烂的和新鲜的总数,再单独获得腐烂的数量。为什么呢?因为当搜索的时候,已腐烂的数量等于总橘子数时,说明已经完成“感染了”这个时候可以返回分钟......
  • 力扣算法
    1.[两数之和]给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。输入:nums=[2,7,11,15],target=9输出:[0,1]暴力法publicint[]TwoSum(int[]nums,inttarget){//第一层循环:遍历数组......
  • 力扣刷题:栈和队列OJ篇(下)
    大家好,这里是小编的博客频道小编的博客:就爱学编程很高兴在CSDN这个大家庭与大家相识,希望能在这里与大家共同进步,共同收获更好的自己!!!目录1.括号匹配问题(1)题目描述(2)解题思路2.循环队列(1)题目描述(2)解题思路快乐的时光总是短暂,咱们下篇博文再见啦!!!如果小编的文章会对......
  • 力扣209. 长度最小的子数组
    给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于等于 target 的长度最小的 子数组 [numsl,numsl+1,...,numsr-1,numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。示例1:输入:target=7,nums=[2,3,1,2,4,3......
  • 力扣 131. 分割回文串
    ......
  • 力扣刷题:栈和队列OJ篇(上)
    大家好,这里是小编的博客频道小编的博客:就爱学编程很高兴在CSDN这个大家庭与大家相识,希望能在这里与大家共同进步,共同收获更好的自己!!!目录1.用队列实现栈(1)题目描述(2)解题思路2.用两个栈实现队列(1)题目描述(2)解题思路快乐的时光总是短暂,咱们下篇博文再见啦!!!如果小编的......
  • 力扣刷题:单链表OJ篇(下)
    大家好,这里是小编的博客频道小编的博客:就爱学编程很高兴在CSDN这个大家庭与大家相识,希望能在这里与大家共同进步,共同收获更好的自己!!!目录1.环形链表(1)题目描述(2)解题思路(3)复杂度分析2.环形链表2(1)题目描述(2)解题思路(3)复杂度分析快乐的时光总是短暂,咱们下篇博文再见啦......
  • 力扣刷题——1847. 最近的房间
    对每个询问,先找出符合实际房间体积,大于询问房间体积房间的房间号,之后再从这些符合条件的候选答案中挑选一个最接近询问房间ID号的。为了找出符合条件的房间,可以对实际房间、询问房间,按照房间体积降序排序,然后只将大于等于询问房间体积的房间ID,加入到待筛选的房间列表中。因为对于......
  • 力扣第四十二题 接雨水(困难难度,c语言附着解析)
    代码如下这个代码是双指针算法,我参考了别人的解法,大致的思路如下,我们先使用两个指针,分别从数组开始和末尾开始遍历,并且我们使用了两个变量,分别记录当前我们遍历到的左边和右边遇到的最大高度。这里为什么要进行height[l]小于或大于的判断再进行相加,根据木桶效应,我们需要......