From 2b59f7e95c132eda4bffc25c66c45bfd5090a9b5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 4 Jul 2023 10:26:59 +0800 Subject: [PATCH] feat: add sql solutions to lc problems * No.2205.The Number of Users That Are Eligible for Discount * No.2292.Products With Three or More Orders in Two Consecutive Years * No.2314.The First Day of the Maximum Recorded Degree in Each City * No.2324.Product Sales Analysis IV * No.2329.Product Sales Analysis V * No.2339.All the Matches of the League * No.2377.Sort the Olympic Table --- .prettierignore | 3 ++- .../README.md | 15 ++++++++++- .../README_EN.md | 11 +++++++- .../Solution.sql | 10 +++++++ .../README.md | 26 +++++++++++++++++++ .../README_EN.md | 26 +++++++++++++++++++ .../Solution.sql | 12 +++++++++ .../README.md | 16 +++++++++++- .../README_EN.md | 16 +++++++++++- .../Solution.sql | 15 +++++++++++ .../2324.Product Sales Analysis IV/README.md | 19 +++++++++++++- .../README_EN.md | 19 +++++++++++++- .../Solution.sql | 18 +++++++++++++ .../2329.Product Sales Analysis V/README.md | 8 +++++- .../README_EN.md | 8 +++++- .../Solution.sql | 7 +++++ .../README.md | 7 ++++- .../README_EN.md | 7 ++++- .../Solution.sql | 6 +++++ .../2377.Sort the Olympic Table/README.md | 5 +++- .../2377.Sort the Olympic Table/README_EN.md | 5 +++- .../2377.Sort the Olympic Table/Solution.sql | 4 +++ 22 files changed, 250 insertions(+), 13 deletions(-) create mode 100644 solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql create mode 100644 solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/Solution.sql create mode 100644 solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/Solution.sql create mode 100644 solution/2300-2399/2324.Product Sales Analysis IV/Solution.sql create mode 100644 solution/2300-2399/2329.Product Sales Analysis V/Solution.sql create mode 100644 solution/2300-2399/2339.All the Matches of the League/Solution.sql create mode 100644 solution/2300-2399/2377.Sort the Olympic Table/Solution.sql diff --git a/.prettierignore b/.prettierignore index e896631c83775..918c0d6aa4098 100644 --- a/.prettierignore +++ b/.prettierignore @@ -24,4 +24,5 @@ node_modules/ /solution/1600-1699/1613.Find the Missing IDs/Solution.sql /solution/1600-1699/1635.Hopper Company Queries I/Solution.sql /solution/1600-1699/1651.Hopper Company Queries III/Solution.sql -/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql \ No newline at end of file +/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql +/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql \ No newline at end of file diff --git a/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README.md b/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README.md index 7b3bf5eb3ae52..1e62fc0b647e1 100644 --- a/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README.md +++ b/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README.md @@ -60,6 +60,10 @@ startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000 +**方法一:使用 count(distinct) 函数** + +注意需要判断的是单次购买金额是否大于等于 `minAmount`,而不是累计购买金额是否大于等于 `minAmount`。 + ### **SQL** @@ -67,7 +71,16 @@ startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000 ```sql - +CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT +BEGIN + RETURN ( + # Write your MySQL query statement below. + # Write your MySQL query statement below. + SELECT count(DISTINCT user_id) AS user_cnt + FROM Purchases + WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount; + ); +END ``` diff --git a/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README_EN.md b/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README_EN.md index b69768623f4cf..784c12ff86a4a 100644 --- a/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README_EN.md +++ b/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/README_EN.md @@ -61,7 +61,16 @@ Out of the three users, only User 3 is eligible for a discount. ### **SQL** ```sql - +CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT +BEGIN + RETURN ( + # Write your MySQL query statement below. + # Write your MySQL query statement below. + SELECT count(DISTINCT user_id) AS user_cnt + FROM Purchases + WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount; + ); +END ``` diff --git a/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql b/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql new file mode 100644 index 0000000000000..402c6dee40c43 --- /dev/null +++ b/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql @@ -0,0 +1,10 @@ +CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT +BEGIN + RETURN ( + # Write your MySQL query statement below. + # Write your MySQL query statement below. + SELECT count(DISTINCT user_id) AS user_cnt + FROM Purchases + WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount; + ); +END \ No newline at end of file diff --git a/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README.md b/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README.md index d68e773204507..f8b92262905df 100644 --- a/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README.md +++ b/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README.md @@ -69,7 +69,33 @@ Orders 表: ```sql +# Write your MySQL query statement below +WITH + P AS ( + SELECT product_id, year(purchase_date) AS y, count(1) >= 3 AS mark + FROM Orders + GROUP BY 1, 2 + ) +SELECT DISTINCT p1.product_id +FROM + P AS p1 + JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id +WHERE p1.mark AND p2.mark; +``` +```sql +# Write your MySQL query statement below +WITH + P AS ( + SELECT product_id, year(purchase_date) AS y + FROM Orders + GROUP BY 1, 2 + HAVING count(1) >= 3 + ) +SELECT DISTINCT p1.product_id +FROM + P AS p1 + JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id; ``` diff --git a/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README_EN.md b/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README_EN.md index 4663242771aa5..09d9e913725cb 100644 --- a/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README_EN.md +++ b/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/README_EN.md @@ -62,7 +62,33 @@ Product 2 was ordered one time in 2022. We do not include it in the answer. ### **SQL** ```sql +# Write your MySQL query statement below +WITH + P AS ( + SELECT product_id, year(purchase_date) AS y, count(1) >= 3 AS mark + FROM Orders + GROUP BY 1, 2 + ) +SELECT DISTINCT p1.product_id +FROM + P AS p1 + JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id +WHERE p1.mark AND p2.mark; +``` +```sql +# Write your MySQL query statement below +WITH + P AS ( + SELECT product_id, year(purchase_date) AS y + FROM Orders + GROUP BY 1, 2 + HAVING count(1) >= 3 + ) +SELECT DISTINCT p1.product_id +FROM + P AS p1 + JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id; ``` diff --git a/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/Solution.sql b/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/Solution.sql new file mode 100644 index 0000000000000..3832e47c27ad9 --- /dev/null +++ b/solution/2200-2299/2292.Products With Three or More Orders in Two Consecutive Years/Solution.sql @@ -0,0 +1,12 @@ +# Write your MySQL query statement below +WITH + P AS ( + SELECT product_id, year(purchase_date) AS y + FROM Orders + GROUP BY 1, 2 + HAVING count(1) >= 3 + ) +SELECT DISTINCT p1.product_id +FROM + P AS p1 + JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id; diff --git a/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README.md b/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README.md index d1815d41fe318..64f5298b5c6e3 100644 --- a/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README.md +++ b/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README.md @@ -72,7 +72,21 @@ Weather 表: ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + rank() OVER ( + PARTITION BY city_id + ORDER BY degree DESC, day + ) AS rk + FROM Weather + ) +SELECT city_id, day, degree +FROM T +WHERE rk = 1 +ORDER BY 1; ``` diff --git a/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README_EN.md b/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README_EN.md index b408e6265b950..c3b548bde44a1 100644 --- a/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README_EN.md +++ b/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/README_EN.md @@ -65,7 +65,21 @@ For city 3, the maximum degree was recorded on 2022-12-07 with -6 degrees. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + rank() OVER ( + PARTITION BY city_id + ORDER BY degree DESC, day + ) AS rk + FROM Weather + ) +SELECT city_id, day, degree +FROM T +WHERE rk = 1 +ORDER BY 1; ``` diff --git a/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/Solution.sql b/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/Solution.sql new file mode 100644 index 0000000000000..8375da603cb73 --- /dev/null +++ b/solution/2300-2399/2314.The First Day of the Maximum Recorded Degree in Each City/Solution.sql @@ -0,0 +1,15 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + rank() OVER ( + PARTITION BY city_id + ORDER BY degree DESC, day + ) AS rk + FROM Weather + ) +SELECT city_id, day, degree +FROM T +WHERE rk = 1 +ORDER BY 1; diff --git a/solution/2300-2399/2324.Product Sales Analysis IV/README.md b/solution/2300-2399/2324.Product Sales Analysis IV/README.md index 6d57c55a18110..555991585770c 100644 --- a/solution/2300-2399/2324.Product Sales Analysis IV/README.md +++ b/solution/2300-2399/2324.Product Sales Analysis IV/README.md @@ -102,7 +102,24 @@ Product 表: ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + user_id, + product_id, + rank() OVER ( + PARTITION BY user_id + ORDER BY sum(quantity * price) DESC + ) AS rk + FROM + Sales + JOIN Product USING (product_id) + GROUP BY 1, 2 + ) +SELECT user_id, product_id +FROM T +WHERE rk = 1; ``` diff --git a/solution/2300-2399/2324.Product Sales Analysis IV/README_EN.md b/solution/2300-2399/2324.Product Sales Analysis IV/README_EN.md index 2f413454d50c8..643ce5a3422fb 100644 --- a/solution/2300-2399/2324.Product Sales Analysis IV/README_EN.md +++ b/solution/2300-2399/2324.Product Sales Analysis IV/README_EN.md @@ -95,7 +95,24 @@ User 102 spent the most money on products 1, 2, and 3. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + user_id, + product_id, + rank() OVER ( + PARTITION BY user_id + ORDER BY sum(quantity * price) DESC + ) AS rk + FROM + Sales + JOIN Product USING (product_id) + GROUP BY 1, 2 + ) +SELECT user_id, product_id +FROM T +WHERE rk = 1; ``` diff --git a/solution/2300-2399/2324.Product Sales Analysis IV/Solution.sql b/solution/2300-2399/2324.Product Sales Analysis IV/Solution.sql new file mode 100644 index 0000000000000..da7f9c80bedaf --- /dev/null +++ b/solution/2300-2399/2324.Product Sales Analysis IV/Solution.sql @@ -0,0 +1,18 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT + user_id, + product_id, + rank() OVER ( + PARTITION BY user_id + ORDER BY sum(quantity * price) DESC + ) AS rk + FROM + Sales + JOIN Product USING (product_id) + GROUP BY 1, 2 + ) +SELECT user_id, product_id +FROM T +WHERE rk = 1; diff --git a/solution/2300-2399/2329.Product Sales Analysis V/README.md b/solution/2300-2399/2329.Product Sales Analysis V/README.md index 312274c1f5e25..820d9e7f85a74 100644 --- a/solution/2300-2399/2329.Product Sales Analysis V/README.md +++ b/solution/2300-2399/2329.Product Sales Analysis V/README.md @@ -93,7 +93,13 @@ Product 表: ```sql - +# Write your MySQL query statement below +SELECT user_id, sum(quantity * price) AS spending +FROM + Sales + JOIN Product USING (product_id) +GROUP BY 1 +ORDER BY 2 DESC, 1; ``` diff --git a/solution/2300-2399/2329.Product Sales Analysis V/README_EN.md b/solution/2300-2399/2329.Product Sales Analysis V/README_EN.md index ff9cc9d17f2df..6e835c438ff2d 100644 --- a/solution/2300-2399/2329.Product Sales Analysis V/README_EN.md +++ b/solution/2300-2399/2329.Product Sales Analysis V/README_EN.md @@ -88,7 +88,13 @@ Users 102 and 103 spent the same amount and we break the tie by their ID while u ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT user_id, sum(quantity * price) AS spending +FROM + Sales + JOIN Product USING (product_id) +GROUP BY 1 +ORDER BY 2 DESC, 1; ``` diff --git a/solution/2300-2399/2329.Product Sales Analysis V/Solution.sql b/solution/2300-2399/2329.Product Sales Analysis V/Solution.sql new file mode 100644 index 0000000000000..e600bbd37eb44 --- /dev/null +++ b/solution/2300-2399/2329.Product Sales Analysis V/Solution.sql @@ -0,0 +1,7 @@ +# Write your MySQL query statement below +SELECT user_id, sum(quantity * price) AS spending +FROM + Sales + JOIN Product USING (product_id) +GROUP BY 1 +ORDER BY 2 DESC, 1; diff --git a/solution/2300-2399/2339.All the Matches of the League/README.md b/solution/2300-2399/2339.All the Matches of the League/README.md index 08dd16a1d1b49..4f2c377767860 100644 --- a/solution/2300-2399/2339.All the Matches of the League/README.md +++ b/solution/2300-2399/2339.All the Matches of the League/README.md @@ -63,7 +63,12 @@ Teams 表: ```sql - +# Write your MySQL query statement below +SELECT t1.team_name AS home_team, t2.team_name AS away_team +FROM + Teams AS t1 + CROSS JOIN Teams AS t2 +WHERE t1.team_name != t2.team_name; ``` diff --git a/solution/2300-2399/2339.All the Matches of the League/README_EN.md b/solution/2300-2399/2339.All the Matches of the League/README_EN.md index 4693dd9a10df6..95af0fb0dc2a5 100644 --- a/solution/2300-2399/2339.All the Matches of the League/README_EN.md +++ b/solution/2300-2399/2339.All the Matches of the League/README_EN.md @@ -58,7 +58,12 @@ Teams table: ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT t1.team_name AS home_team, t2.team_name AS away_team +FROM + Teams AS t1 + CROSS JOIN Teams AS t2 +WHERE t1.team_name != t2.team_name; ``` diff --git a/solution/2300-2399/2339.All the Matches of the League/Solution.sql b/solution/2300-2399/2339.All the Matches of the League/Solution.sql new file mode 100644 index 0000000000000..86627a42718cd --- /dev/null +++ b/solution/2300-2399/2339.All the Matches of the League/Solution.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below +SELECT t1.team_name AS home_team, t2.team_name AS away_team +FROM + Teams AS t1 + CROSS JOIN Teams AS t2 +WHERE t1.team_name != t2.team_name; diff --git a/solution/2300-2399/2377.Sort the Olympic Table/README.md b/solution/2300-2399/2377.Sort the Olympic Table/README.md index 3b89f1d06d574..2491f71594edd 100644 --- a/solution/2300-2399/2377.Sort the Olympic Table/README.md +++ b/solution/2300-2399/2377.Sort the Olympic Table/README.md @@ -77,7 +77,10 @@ Olympic 表: ```sql - +# Write your MySQL query statement below +SELECT * +FROM Olympic +ORDER BY 2 DESC, 3 DESC, 4 DESC, 1; ``` diff --git a/solution/2300-2399/2377.Sort the Olympic Table/README_EN.md b/solution/2300-2399/2377.Sort the Olympic Table/README_EN.md index 543d4dcfa0e0e..1a2fd7fe0f80d 100644 --- a/solution/2300-2399/2377.Sort the Olympic Table/README_EN.md +++ b/solution/2300-2399/2377.Sort the Olympic Table/README_EN.md @@ -71,7 +71,10 @@ Israel comes before Egypt because it has more bronze medals. ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT * +FROM Olympic +ORDER BY 2 DESC, 3 DESC, 4 DESC, 1; ``` diff --git a/solution/2300-2399/2377.Sort the Olympic Table/Solution.sql b/solution/2300-2399/2377.Sort the Olympic Table/Solution.sql new file mode 100644 index 0000000000000..cded51a6f81f9 --- /dev/null +++ b/solution/2300-2399/2377.Sort the Olympic Table/Solution.sql @@ -0,0 +1,4 @@ +# Write your MySQL query statement below +SELECT * +FROM Olympic +ORDER BY 2 DESC, 3 DESC, 4 DESC, 1;