From fd2c47d0b6aa7e5ebea6fff271e4e141424a2c01 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 2 Jul 2023 17:48:39 +0800 Subject: [PATCH] feat: add sql solutions to lc problems * No.1990.Count the Number of Experiments * No.2020.Number of Accounts That Did Not Stream * No.2051.The Category of Each Member in the Store * No.2066.Account Balance * No.2084.Drop Type 1 Orders for Customers With Type 0 Orders * No.2112.The Airport With the Most Traffic --- .prettierrc | 1 + .../0100-0199/0192.Word Frequency/README.md | 3 +- .../0192.Word Frequency/README_EN.md | 3 +- .../0100-0199/0192.Word Frequency/Solution.sh | 19 ++----------- .../README.md | 28 ++++++++++++++++++- .../README_EN.md | 28 ++++++++++++++++++- .../Solution.sql | 27 ++++++++++++++++++ .../README.md | 10 ++++++- .../README_EN.md | 10 ++++++- .../Solution.sql | 9 ++++++ .../README.md | 9 +++++- .../README_EN.md | 9 +++++- .../Solution.sql | 8 ++++++ .../README.md | 18 +++++++++++- .../README_EN.md | 18 +++++++++++- .../Solution.sql | 17 +++++++++++ .../2000-2099/2066.Account Balance/README.md | 11 +++++++- .../2066.Account Balance/README_EN.md | 11 +++++++- .../2066.Account Balance/Solution.sql | 10 +++++++ .../README.md | 13 ++++++++- .../README_EN.md | 13 ++++++++- .../Solution.sql | 12 ++++++++ .../README.md | 16 ++++++++++- .../README_EN.md | 16 ++++++++++- .../Solution.sql | 15 ++++++++++ 25 files changed, 301 insertions(+), 33 deletions(-) create mode 100644 solution/1900-1999/1990.Count the Number of Experiments/Solution.sql create mode 100644 solution/2000-2099/2020.Number of Accounts That Did Not Stream/Solution.sql create mode 100644 solution/2000-2099/2041.Accepted Candidates From the Interviews/Solution.sql create mode 100644 solution/2000-2099/2051.The Category of Each Member in the Store/Solution.sql create mode 100644 solution/2000-2099/2066.Account Balance/Solution.sql create mode 100644 solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/Solution.sql create mode 100644 solution/2100-2199/2112.The Airport With the Most Traffic/Solution.sql diff --git a/.prettierrc b/.prettierrc index 16b9319f0e525..cad992f66afcf 100644 --- a/.prettierrc +++ b/.prettierrc @@ -33,6 +33,7 @@ "solution/1600-1699/1667.Fix Names in a Table/Solution.sql", "solution/1700-1799/1777.Product's Price for Each Store/Solution.sql", "solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql", + "solution/2000-2099/2066.Account Balance/Solution.sql", "solution/2600-2699/2686.Immediate Food Delivery III/Solution.sql", "solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql" ], diff --git a/solution/0100-0199/0192.Word Frequency/README.md b/solution/0100-0199/0192.Word Frequency/README.md index 4ccfcde9e4dd2..c6f71035bbaab 100644 --- a/solution/0100-0199/0192.Word Frequency/README.md +++ b/solution/0100-0199/0192.Word Frequency/README.md @@ -50,7 +50,8 @@ day 1 ```sh - +# Read from the file words.txt and output the word frequency list to stdout. +cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}' ``` diff --git a/solution/0100-0199/0192.Word Frequency/README_EN.md b/solution/0100-0199/0192.Word Frequency/README_EN.md index 86f91a63baa13..2dbcd6e7b0d30 100644 --- a/solution/0100-0199/0192.Word Frequency/README_EN.md +++ b/solution/0100-0199/0192.Word Frequency/README_EN.md @@ -46,7 +46,8 @@ day 1 ### **Bash** ```sh - +# Read from the file words.txt and output the word frequency list to stdout. +cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}' ``` diff --git a/solution/0100-0199/0192.Word Frequency/Solution.sh b/solution/0100-0199/0192.Word Frequency/Solution.sh index 4c499bd209955..1f792cf92fa3a 100644 --- a/solution/0100-0199/0192.Word Frequency/Solution.sh +++ b/solution/0100-0199/0192.Word Frequency/Solution.sh @@ -1,17 +1,2 @@ -#!/usr/bin/env bash - -# NF是当前行的field字段数;NR是正在处理的当前行数。 -awk '{ - for(i=1;i<=NF;i++){ - res[$i]++ - } -} -END{ - for(k in res){ - print k,res[k] - } -}' words.txt | sort -nr -k2 - -#or: - -cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2" "$1}' \ No newline at end of file +# Read from the file words.txt and output the word frequency list to stdout. +cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}' \ No newline at end of file diff --git a/solution/1900-1999/1990.Count the Number of Experiments/README.md b/solution/1900-1999/1990.Count the Number of Experiments/README.md index cf963c26dc6f0..40af4c03f07b3 100644 --- a/solution/1900-1999/1990.Count the Number of Experiments/README.md +++ b/solution/1900-1999/1990.Count the Number of Experiments/README.md @@ -79,7 +79,33 @@ Experiments table: ```sql - +# Write your MySQL query statement below +WITH + P AS ( + SELECT 'Android' AS platform + UNION + SELECT 'IOS' + UNION + SELECT 'Web' + ), + Exp AS ( + SELECT 'Reading' AS experiment_name + UNION + SELECT 'Sports' + UNION + SELECT 'Programming' + ), + T AS ( + SELECT * + FROM + P, + Exp + ) +SELECT platform, experiment_name, count(experiment_id) AS num_experiments +FROM + T AS t + LEFT JOIN Experiments USING (platform, experiment_name) +GROUP BY 1, 2; ``` diff --git a/solution/1900-1999/1990.Count the Number of Experiments/README_EN.md b/solution/1900-1999/1990.Count the Number of Experiments/README_EN.md index 391b1475a6616..4cd4b3530f34e 100644 --- a/solution/1900-1999/1990.Count the Number of Experiments/README_EN.md +++ b/solution/1900-1999/1990.Count the Number of Experiments/README_EN.md @@ -71,7 +71,33 @@ On the platform "Web", we had two "Reading" experiments and ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + P AS ( + SELECT 'Android' AS platform + UNION + SELECT 'IOS' + UNION + SELECT 'Web' + ), + Exp AS ( + SELECT 'Reading' AS experiment_name + UNION + SELECT 'Sports' + UNION + SELECT 'Programming' + ), + T AS ( + SELECT * + FROM + P, + Exp + ) +SELECT platform, experiment_name, count(experiment_id) AS num_experiments +FROM + T AS t + LEFT JOIN Experiments USING (platform, experiment_name) +GROUP BY 1, 2; ``` diff --git a/solution/1900-1999/1990.Count the Number of Experiments/Solution.sql b/solution/1900-1999/1990.Count the Number of Experiments/Solution.sql new file mode 100644 index 0000000000000..3f0d7af794644 --- /dev/null +++ b/solution/1900-1999/1990.Count the Number of Experiments/Solution.sql @@ -0,0 +1,27 @@ +# Write your MySQL query statement below +WITH + P AS ( + SELECT 'Android' AS platform + UNION + SELECT 'IOS' + UNION + SELECT 'Web' + ), + Exp AS ( + SELECT 'Reading' AS experiment_name + UNION + SELECT 'Sports' + UNION + SELECT 'Programming' + ), + T AS ( + SELECT * + FROM + P, + Exp + ) +SELECT platform, experiment_name, count(experiment_id) AS num_experiments +FROM + T AS t + LEFT JOIN Experiments USING (platform, experiment_name) +GROUP BY 1, 2; diff --git a/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README.md b/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README.md index aab27ce55f0a0..1995bd3533f25 100644 --- a/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README.md +++ b/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README.md @@ -89,7 +89,15 @@ Streams table: ```sql - +# Write your MySQL query statement below +SELECT count(DISTINCT sub.account_id) AS accounts_count +FROM + Subscriptions AS sub + LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id +WHERE + year(start_date) <= 2021 + AND year(end_date) >= 2021 + AND (year(stream_date) != 2021 OR stream_date > end_date); ``` diff --git a/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README_EN.md b/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README_EN.md index 3778a472216d7..f7ddfccd7535d 100644 --- a/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README_EN.md +++ b/solution/2000-2099/2020.Number of Accounts That Did Not Stream/README_EN.md @@ -86,7 +86,15 @@ User 11 did not subscribe in 2021. ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT count(DISTINCT sub.account_id) AS accounts_count +FROM + Subscriptions AS sub + LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id +WHERE + year(start_date) <= 2021 + AND year(end_date) >= 2021 + AND (year(stream_date) != 2021 OR stream_date > end_date); ``` diff --git a/solution/2000-2099/2020.Number of Accounts That Did Not Stream/Solution.sql b/solution/2000-2099/2020.Number of Accounts That Did Not Stream/Solution.sql new file mode 100644 index 0000000000000..417e03701d268 --- /dev/null +++ b/solution/2000-2099/2020.Number of Accounts That Did Not Stream/Solution.sql @@ -0,0 +1,9 @@ +# Write your MySQL query statement below +SELECT count(DISTINCT sub.account_id) AS accounts_count +FROM + Subscriptions AS sub + LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id +WHERE + year(start_date) <= 2021 + AND year(end_date) >= 2021 + AND (year(stream_date) != 2021 OR stream_date > end_date); diff --git a/solution/2000-2099/2041.Accepted Candidates From the Interviews/README.md b/solution/2000-2099/2041.Accepted Candidates From the Interviews/README.md index f4e18feb0ed54..e1284f84ed4e2 100644 --- a/solution/2000-2099/2041.Accepted Candidates From the Interviews/README.md +++ b/solution/2000-2099/2041.Accepted Candidates From the Interviews/README.md @@ -101,7 +101,14 @@ Rounds table: ```sql - +# Write your MySQL query statement below +SELECT candidate_id +FROM + Candidates AS c + LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id +WHERE years_of_exp >= 2 +GROUP BY c.interview_id +HAVING sum(score) > 15; ``` diff --git a/solution/2000-2099/2041.Accepted Candidates From the Interviews/README_EN.md b/solution/2000-2099/2041.Accepted Candidates From the Interviews/README_EN.md index 370c3a2f4066a..ad2eb23d93e5f 100644 --- a/solution/2000-2099/2041.Accepted Candidates From the Interviews/README_EN.md +++ b/solution/2000-2099/2041.Accepted Candidates From the Interviews/README_EN.md @@ -94,7 +94,14 @@ Rounds table: ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT candidate_id +FROM + Candidates AS c + LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id +WHERE years_of_exp >= 2 +GROUP BY c.interview_id +HAVING sum(score) > 15; ``` diff --git a/solution/2000-2099/2041.Accepted Candidates From the Interviews/Solution.sql b/solution/2000-2099/2041.Accepted Candidates From the Interviews/Solution.sql new file mode 100644 index 0000000000000..48f4af5db54fa --- /dev/null +++ b/solution/2000-2099/2041.Accepted Candidates From the Interviews/Solution.sql @@ -0,0 +1,8 @@ +# Write your MySQL query statement below +SELECT candidate_id +FROM + Candidates AS c + LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id +WHERE years_of_exp >= 2 +GROUP BY c.interview_id +HAVING sum(score) > 15; diff --git a/solution/2000-2099/2051.The Category of Each Member in the Store/README.md b/solution/2000-2099/2051.The Category of Each Member in the Store/README.md index 00b16fd25b03d..def4d66fee53b 100644 --- a/solution/2000-2099/2051.The Category of Each Member in the Store/README.md +++ b/solution/2000-2099/2051.The Category of Each Member in the Store/README.md @@ -135,7 +135,23 @@ Purchases 表: ```sql - +# Write your MySQL query statement below +SELECT + m.member_id, + name, + CASE + WHEN count(v.visit_id) = 0 THEN 'Bronze' + WHEN 100 * count(charged_amount) / count( + v.visit_id + ) >= 80 THEN 'Diamond' + WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold' + ELSE 'Silver' + END AS category +FROM + Members AS m + LEFT JOIN Visits AS v ON m.member_id = v.member_id + LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id +GROUP BY member_id; ``` diff --git a/solution/2000-2099/2051.The Category of Each Member in the Store/README_EN.md b/solution/2000-2099/2051.The Category of Each Member in the Store/README_EN.md index 65341ea4963f4..ea43dea3b5752 100644 --- a/solution/2000-2099/2051.The Category of Each Member in the Store/README_EN.md +++ b/solution/2000-2099/2051.The Category of Each Member in the Store/README_EN.md @@ -129,7 +129,23 @@ Purchases table: ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT + m.member_id, + name, + CASE + WHEN count(v.visit_id) = 0 THEN 'Bronze' + WHEN 100 * count(charged_amount) / count( + v.visit_id + ) >= 80 THEN 'Diamond' + WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold' + ELSE 'Silver' + END AS category +FROM + Members AS m + LEFT JOIN Visits AS v ON m.member_id = v.member_id + LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id +GROUP BY member_id; ``` diff --git a/solution/2000-2099/2051.The Category of Each Member in the Store/Solution.sql b/solution/2000-2099/2051.The Category of Each Member in the Store/Solution.sql new file mode 100644 index 0000000000000..7dbd04cd60fed --- /dev/null +++ b/solution/2000-2099/2051.The Category of Each Member in the Store/Solution.sql @@ -0,0 +1,17 @@ +# Write your MySQL query statement below +SELECT + m.member_id, + name, + CASE + WHEN count(v.visit_id) = 0 THEN 'Bronze' + WHEN 100 * count(charged_amount) / count( + v.visit_id + ) >= 80 THEN 'Diamond' + WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold' + ELSE 'Silver' + END AS category +FROM + Members AS m + LEFT JOIN Visits AS v ON m.member_id = v.member_id + LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id +GROUP BY member_id; diff --git a/solution/2000-2099/2066.Account Balance/README.md b/solution/2000-2099/2066.Account Balance/README.md index 89eb4195a9736..41722b4735ca7 100644 --- a/solution/2000-2099/2066.Account Balance/README.md +++ b/solution/2000-2099/2066.Account Balance/README.md @@ -80,7 +80,16 @@ Transactions 表: ```sql - +# Write your MySQL query statement below +SELECT + account_id, + day, + sum(if(type = 'Deposit', amount, -amount)) OVER ( + PARTITION BY account_id + ORDER BY day + ) AS balance +FROM Transactions +ORDER BY 1, 2; ``` diff --git a/solution/2000-2099/2066.Account Balance/README_EN.md b/solution/2000-2099/2066.Account Balance/README_EN.md index eb3a1d9662384..ac5dea0076e4e 100644 --- a/solution/2000-2099/2066.Account Balance/README_EN.md +++ b/solution/2000-2099/2066.Account Balance/README_EN.md @@ -72,7 +72,16 @@ Account 2: ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT + account_id, + day, + sum(if(type = 'Deposit', amount, -amount)) OVER ( + PARTITION BY account_id + ORDER BY day + ) AS balance +FROM Transactions +ORDER BY 1, 2; ``` diff --git a/solution/2000-2099/2066.Account Balance/Solution.sql b/solution/2000-2099/2066.Account Balance/Solution.sql new file mode 100644 index 0000000000000..a9a3d833eecb8 --- /dev/null +++ b/solution/2000-2099/2066.Account Balance/Solution.sql @@ -0,0 +1,10 @@ +# Write your MySQL query statement below +SELECT + account_id, + day, + sum(if(type = 'Deposit', amount, -amount)) OVER ( + PARTITION BY account_id + ORDER BY day + ) AS balance +FROM Transactions +ORDER BY 1, 2; diff --git a/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README.md b/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README.md index f7a9cf4d91d22..5a76d042ddc23 100644 --- a/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README.md +++ b/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README.md @@ -80,7 +80,18 @@ Orders table: ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT DISTINCT customer_id + FROM Orders + WHERE order_type = 0 + ) +SELECT * +FROM Orders AS o +WHERE + order_type = 0 + OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id); ``` diff --git a/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README_EN.md b/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README_EN.md index 4f6dc49891981..249fa9675b109 100644 --- a/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README_EN.md +++ b/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README_EN.md @@ -76,7 +76,18 @@ Customer 4 has two orders of type 1. We return both of them. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT DISTINCT customer_id + FROM Orders + WHERE order_type = 0 + ) +SELECT * +FROM Orders AS o +WHERE + order_type = 0 + OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id); ``` diff --git a/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/Solution.sql b/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/Solution.sql new file mode 100644 index 0000000000000..65e5bd7beedf3 --- /dev/null +++ b/solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/Solution.sql @@ -0,0 +1,12 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT DISTINCT customer_id + FROM Orders + WHERE order_type = 0 + ) +SELECT * +FROM Orders AS o +WHERE + order_type = 0 + OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id); diff --git a/solution/2100-2199/2112.The Airport With the Most Traffic/README.md b/solution/2100-2199/2112.The Airport With the Most Traffic/README.md index 19d9744b67c29..e5a0314723923 100644 --- a/solution/2100-2199/2112.The Airport With the Most Traffic/README.md +++ b/solution/2100-2199/2112.The Airport With the Most Traffic/README.md @@ -98,7 +98,21 @@ Flights 表: ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT * FROM Flights + UNION + SELECT arrival_airport, departure_airport, flights_count FROM Flights + ), + P AS ( + SELECT departure_airport, sum(flights_count) AS cnt + FROM T + GROUP BY 1 + ) +SELECT departure_airport AS airport_id +FROM P +WHERE cnt = (SELECT max(cnt) FROM P); ``` diff --git a/solution/2100-2199/2112.The Airport With the Most Traffic/README_EN.md b/solution/2100-2199/2112.The Airport With the Most Traffic/README_EN.md index 26fdb2ab597c5..c82902c301f04 100644 --- a/solution/2100-2199/2112.The Airport With the Most Traffic/README_EN.md +++ b/solution/2100-2199/2112.The Airport With the Most Traffic/README_EN.md @@ -92,7 +92,21 @@ The airports with the most traffic are airports 1, 2, 3, and 4. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT * FROM Flights + UNION + SELECT arrival_airport, departure_airport, flights_count FROM Flights + ), + P AS ( + SELECT departure_airport, sum(flights_count) AS cnt + FROM T + GROUP BY 1 + ) +SELECT departure_airport AS airport_id +FROM P +WHERE cnt = (SELECT max(cnt) FROM P); ``` diff --git a/solution/2100-2199/2112.The Airport With the Most Traffic/Solution.sql b/solution/2100-2199/2112.The Airport With the Most Traffic/Solution.sql new file mode 100644 index 0000000000000..8eca6249113aa --- /dev/null +++ b/solution/2100-2199/2112.The Airport With the Most Traffic/Solution.sql @@ -0,0 +1,15 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT * FROM Flights + UNION + SELECT arrival_airport, departure_airport, flights_count FROM Flights + ), + P AS ( + SELECT departure_airport, sum(flights_count) AS cnt + FROM T + GROUP BY 1 + ) +SELECT departure_airport AS airport_id +FROM P +WHERE cnt = (SELECT max(cnt) FROM P);