From 659cbe986bc66bf37c4a66379ec0714732cde5b5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 7 Jul 2023 09:24:36 +0800 Subject: [PATCH 1/3] feat: add sql solution to lc problem: No.1645 No.1645.Hopper Company Queries II --- .../1645.Hopper Company Queries II/README.md | 52 ++++++++++++++++++- .../README_EN.md | 42 ++++++++++++++- .../Solution.sql | 41 +++++++++++++++ 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 solution/1600-1699/1645.Hopper Company Queries II/Solution.sql diff --git a/solution/1600-1699/1645.Hopper Company Queries II/README.md b/solution/1600-1699/1645.Hopper Company Queries II/README.md index 48041dacc6682..57d2f4a9a5885 100644 --- a/solution/1600-1699/1645.Hopper Company Queries II/README.md +++ b/solution/1600-1699/1645.Hopper Company Queries II/README.md @@ -151,6 +151,16 @@ ride_id是该表的主键。 +**方法一:递归 + 窗口函数** + +我们可以使用递归的方法生成 $1 \sim 12$ 月的数据,记录在 `Month` 表中。 + +接下来,我们用 `Month` 表与 `Drivers` 表进行左连接,连接的条件是 `year(d.join_date) < 2020 or (year(d.join_date) = 2020 and month(d.join_date) <= month)`,这样就可以得到每个月的活跃司机数。 + +然后,我们再用 `Rides` 表与 `AcceptedRides` 表进行内连接,连接的条件是 `ride_id` 相等,并且我们只查出 `year(requested_at) = 2020` 的数据,这样就可以得到 $2020$ 年被接受的所有行程。 + +最后,我们将上面两个表进行左连接,连接的条件是 `month` 相等、`driver_id` 相等,并且 `join_date` 小于等于 `requested_at`,这样就可以得到每个月被接受的行程数,按月份进行分组,就可以得到每个月的活跃司机数和被接受的行程数,从而计算出每个月的接单率。 + ### **SQL** @@ -158,7 +168,47 @@ ride_id是该表的主键。 ```sql - +# Write your MySQL query statement below +WITH RECURSIVE + Month AS ( + SELECT 1 AS month + UNION + SELECT month + 1 + FROM Month + WHERE month < 12 + ), + S AS ( + SELECT month, driver_id, join_date + FROM + Month AS m + LEFT JOIN Drivers AS d + ON year(d.join_date) < 2020 + OR (year(d.join_date) = 2020 AND month(d.join_date) <= month) + ORDER BY 1, 3 + ), + T AS ( + SELECT driver_id, requested_at + FROM + Rides + JOIN AcceptedRides USING (ride_id) + WHERE year(requested_at) = 2020 + ) +SELECT + month, + ifnull( + round( + count(DISTINCT t.driver_id) * 100 / count(DISTINCT s.driver_id), + 2 + ), + 0 + ) AS working_percentage +FROM + S AS s + LEFT JOIN T AS t + ON s.driver_id = t.driver_id + AND s.join_date <= t.requested_at + AND s.month = month(t.requested_at) +GROUP BY 1; ``` diff --git a/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md b/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md index 107703c3303a0..2002e7f5e01fc 100644 --- a/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md +++ b/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md @@ -154,7 +154,47 @@ By the end of December --> six active drivers (10, 8, 5, 7, 4, 1) and one acc ### **SQL** ```sql - +# Write your MySQL query statement below +WITH RECURSIVE + Month AS ( + SELECT 1 AS month + UNION + SELECT month + 1 + FROM Month + WHERE month < 12 + ), + S AS ( + SELECT month, driver_id, join_date + FROM + Month AS m + LEFT JOIN Drivers AS d + ON year(d.join_date) < 2020 + OR (year(d.join_date) = 2020 AND month(d.join_date) <= month) + ORDER BY 1, 3 + ), + T AS ( + SELECT driver_id, requested_at + FROM + Rides + JOIN AcceptedRides USING (ride_id) + WHERE year(requested_at) = 2020 + ) +SELECT + month, + ifnull( + round( + count(DISTINCT t.driver_id) * 100 / count(DISTINCT s.driver_id), + 2 + ), + 0 + ) AS working_percentage +FROM + S AS s + LEFT JOIN T AS t + ON s.driver_id = t.driver_id + AND s.join_date <= t.requested_at + AND s.month = month(t.requested_at) +GROUP BY 1; ``` diff --git a/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql b/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql new file mode 100644 index 0000000000000..e188bf00a56de --- /dev/null +++ b/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql @@ -0,0 +1,41 @@ +# Write your MySQL query statement below +WITH RECURSIVE + Month AS ( + SELECT 1 AS month + UNION + SELECT month + 1 + FROM Month + WHERE month < 12 + ), + S AS ( + SELECT month, driver_id, join_date + FROM + Month AS m + LEFT JOIN Drivers AS d + ON year(d.join_date) < 2020 + OR (year(d.join_date) = 2020 AND month(d.join_date) <= month) + ORDER BY 1, 3 + ), + T AS ( + SELECT driver_id, requested_at + FROM + Rides + JOIN AcceptedRides USING (ride_id) + WHERE year(requested_at) = 2020 + ) +SELECT + month, + ifnull( + round( + count(DISTINCT t.driver_id) * 100 / count(DISTINCT s.driver_id), + 2 + ), + 0 + ) AS working_percentage +FROM + S AS s + LEFT JOIN T AS t + ON s.driver_id = t.driver_id + AND s.join_date <= t.requested_at + AND s.month = month(t.requested_at) +GROUP BY 1; From 8d4e26698c0c6613997ab58c6000b382d1f72cdd Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 7 Jul 2023 09:29:11 +0800 Subject: [PATCH 2/3] fix: solution --- solution/1600-1699/1645.Hopper Company Queries II/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/1600-1699/1645.Hopper Company Queries II/README.md b/solution/1600-1699/1645.Hopper Company Queries II/README.md index 57d2f4a9a5885..b5d65ba929b07 100644 --- a/solution/1600-1699/1645.Hopper Company Queries II/README.md +++ b/solution/1600-1699/1645.Hopper Company Queries II/README.md @@ -151,7 +151,7 @@ ride_id是该表的主键。 -**方法一:递归 + 窗口函数** +**方法一:递归 + 左连接 + 分组** 我们可以使用递归的方法生成 $1 \sim 12$ 月的数据,记录在 `Month` 表中。 From f5cb01553f4b3513808d580fed3b56371a6bd321 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 7 Jul 2023 09:32:01 +0800 Subject: [PATCH 3/3] fix: solution --- solution/1600-1699/1645.Hopper Company Queries II/README.md | 1 - solution/1600-1699/1645.Hopper Company Queries II/README_EN.md | 1 - solution/1600-1699/1645.Hopper Company Queries II/Solution.sql | 1 - 3 files changed, 3 deletions(-) diff --git a/solution/1600-1699/1645.Hopper Company Queries II/README.md b/solution/1600-1699/1645.Hopper Company Queries II/README.md index b5d65ba929b07..bea1db99251a0 100644 --- a/solution/1600-1699/1645.Hopper Company Queries II/README.md +++ b/solution/1600-1699/1645.Hopper Company Queries II/README.md @@ -184,7 +184,6 @@ WITH RECURSIVE LEFT JOIN Drivers AS d ON year(d.join_date) < 2020 OR (year(d.join_date) = 2020 AND month(d.join_date) <= month) - ORDER BY 1, 3 ), T AS ( SELECT driver_id, requested_at diff --git a/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md b/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md index 2002e7f5e01fc..88598343c5250 100644 --- a/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md +++ b/solution/1600-1699/1645.Hopper Company Queries II/README_EN.md @@ -170,7 +170,6 @@ WITH RECURSIVE LEFT JOIN Drivers AS d ON year(d.join_date) < 2020 OR (year(d.join_date) = 2020 AND month(d.join_date) <= month) - ORDER BY 1, 3 ), T AS ( SELECT driver_id, requested_at diff --git a/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql b/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql index e188bf00a56de..7d414186e6d93 100644 --- a/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql +++ b/solution/1600-1699/1645.Hopper Company Queries II/Solution.sql @@ -14,7 +14,6 @@ WITH RECURSIVE LEFT JOIN Drivers AS d ON year(d.join_date) < 2020 OR (year(d.join_date) = 2020 AND month(d.join_date) <= month) - ORDER BY 1, 3 ), T AS ( SELECT driver_id, requested_at