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

1651. Hopper 公司查询 III - 力扣(LeetCode)

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

  1. 1651. Hopper 公司查询 III - 力扣(LeetCode)

  2. 目标

    1. 输入

      表:AcceptedRides
      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
      表:Rides
      ride_iddriver_idride_distanceride_duration
      10106338
      13107396
      7810028
      17711968
      20112192
      5742101
      24638
      1183743
      15810882
      1283834
      1419074
      表: 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
    2. 输出

      输出
      monthaverage_ride_distanceaverage_ride_duration
      12112.67
      22112.67
      32112.67
      424.3332
      557.6741.33
      697.3364
      77332
      839.6722.67
      954.3364.33
      1056.3377
  3. 分析

    编写一个解决方案,计算出从 2020 年 1 月至 3 月 至 2020 年 10 月至 12 月 的每三个月窗口的 average_ride_distance 和 average_ride_duration 。并将 average_ride_distance 和 average_ride_duration 四舍五入至 小数点后两位 。
    通过将三个月的总 ride_distance 相加并除以 3 来计算 average_ride_distance 。average_ride_duration 的计算方法与此类似。
    返回按 month 升序排列的结果表,其中 month 是起始月份的编号(一月为 1,二月为 2 ...)。
    表:AcceptedRides表:Rides表: Drivers输出
    ride_iduser_idrequested_atride_iddriver_idride_distanceride_durationdriver_idjoin_datemonthaverage_ride_distanceaverage_ride_duration
    6752019/12/910106338102019/12/1012112.67
    1542020/2/91310739682020/1/1322112.67
    10632020/3/4781002852020/2/1632112.67
    19392020/4/61771196872020/3/8424.3332
    3412020/6/32011219242020/5/17557.6741.33
    13522020/6/22574210112020/10/24697.3364
    7692020/7/162463862021/1/577332
    17702020/8/251183743839.6722.67
    20812020/11/215810882954.3364.33
    5572020/11/912838341056.3377
    2422020/12/91419074
    11682021/1/11
    15322021/1/17
    12112021/1/19
    14182021/1/27
    递归月份month计算每月的距离和时间monthride_distanceride_duration计算每3个月的平均距离和时间monthaverage_ride_distanceaverage_ride_duration
    110012112.67
    220022112.67
    33633832112.67
    4400424.3332
    5500557.6741.33
    667396697.3364
    771002877332
    8811968839.6722.67
    9900954.3364.33
    1010001056.3377
    1111163193
    1212638
  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 DISTINCT
        month,
        SUM(IFNULL(ride_distance, 0)) ride_distance,
        SUM(IFNULL(ride_duration, 0)) ride_duration
    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
    ORDER BY month
    ;
    # 计算每3个月的平均距离和时间
    WITH RECURSIVE
        T1 (month) AS (SELECT
                           1
                       FROM dual
                       UNION ALL
                       SELECT
                           month + 1
                       FROM T1
                       WHERE month < 12),
        T2 AS (SELECT DISTINCT
                   month,
                   SUM(IFNULL(ride_distance, 0)) ride_distance,
                   SUM(IFNULL(ride_duration, 0)) ride_duration
               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
               ORDER BY month)
    ,T3 AS (  SELECT month,ROUND( SUM(IFNULL(ride_distance, 0)) OVER (ORDER BY month ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) /
        3,2) average_ride_distance,
        ROUND(SUM(IFNULL(ride_duration, 0)) OVER (ORDER BY month ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) /
        3,2) average_ride_duration
               FROM T2 )
    SELECT * FROM T3 WHERE month<11

  5. 小结

        递归,窗口函数,多表连接

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

相关文章

  • 1645. Hopper 公司查询 II - 力扣(LeetCode)
    1645.Hopper公司查询II-力扣(LeetCode)目标输入表Rides:ride_iduser_idrequested_at6752019/12/91542020/2/910632020/3/419392020/4/63412020/6/313522020/6/227692020/7/1617702020/8/2520812020/11/25572020/11/92422020/12/911682021/1/1115322021/1/1712112021/......
  • 力扣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,加入到待筛选的房间列表中。因为对于......