diff --git a/solution/1100-1199/1127.User Purchase Platform/README.md b/solution/1100-1199/1127.User Purchase Platform/README.md index ec564c94da459..26cb5540583b8 100644 --- a/solution/1100-1199/1127.User Purchase Platform/README.md +++ b/solution/1100-1199/1127.User Purchase Platform/README.md @@ -65,29 +65,29 @@ Result table: ```sql # Write your MySQL query statement below WITH - s AS ( + P AS ( SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending UNION - SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending + SELECT DISTINCT spend_date, 'mobile' FROM Spending UNION - SELECT DISTINCT spend_date, 'both' AS platform FROM Spending + SELECT DISTINCT spend_date, 'both' FROM Spending ), - t AS ( + T AS ( SELECT user_id, spend_date, - if(count(platform) = 2, 'both', platform) AS platform, - sum(amount) AS amount + sum(amount) AS amount, + if(count(platform) = 1, platform, 'both') AS platform FROM Spending GROUP BY 1, 2 ) SELECT - t1.*, + p.*, ifnull(sum(amount), 0) AS total_amount, - count(t2.user_id) AS total_users + count(t.user_id) AS total_users FROM - s AS t1 - LEFT JOIN t AS t2 USING (spend_date, platform) + P AS p + LEFT JOIN T AS t USING (spend_date, platform) GROUP BY 1, 2; ``` diff --git a/solution/1100-1199/1127.User Purchase Platform/README_EN.md b/solution/1100-1199/1127.User Purchase Platform/README_EN.md index c225a876d59a1..915af2c8a0b0c 100644 --- a/solution/1100-1199/1127.User Purchase Platform/README_EN.md +++ b/solution/1100-1199/1127.User Purchase Platform/README_EN.md @@ -69,29 +69,29 @@ On 2019-07-02, user 2 purchased using mobile only, user 3 purch ```sql # Write your MySQL query statement below WITH - s AS ( + P AS ( SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending UNION - SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending + SELECT DISTINCT spend_date, 'mobile' FROM Spending UNION - SELECT DISTINCT spend_date, 'both' AS platform FROM Spending + SELECT DISTINCT spend_date, 'both' FROM Spending ), - t AS ( + T AS ( SELECT user_id, spend_date, - if(count(platform) = 2, 'both', platform) AS platform, - sum(amount) AS amount + sum(amount) AS amount, + if(count(platform) = 1, platform, 'both') AS platform FROM Spending GROUP BY 1, 2 ) SELECT - t1.*, + p.*, ifnull(sum(amount), 0) AS total_amount, - count(t2.user_id) AS total_users + count(t.user_id) AS total_users FROM - s AS t1 - LEFT JOIN t AS t2 USING (spend_date, platform) + P AS p + LEFT JOIN T AS t USING (spend_date, platform) GROUP BY 1, 2; ``` diff --git a/solution/1100-1199/1127.User Purchase Platform/Solution.sql b/solution/1100-1199/1127.User Purchase Platform/Solution.sql index 540c6f39e1e0d..ce96567abb255 100644 --- a/solution/1100-1199/1127.User Purchase Platform/Solution.sql +++ b/solution/1100-1199/1127.User Purchase Platform/Solution.sql @@ -1,26 +1,26 @@ # Write your MySQL query statement below WITH - s AS ( + P AS ( SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending UNION - SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending + SELECT DISTINCT spend_date, 'mobile' FROM Spending UNION - SELECT DISTINCT spend_date, 'both' AS platform FROM Spending + SELECT DISTINCT spend_date, 'both' FROM Spending ), - t AS ( + T AS ( SELECT user_id, spend_date, - if(count(platform) = 2, 'both', platform) AS platform, - sum(amount) AS amount + sum(amount) AS amount, + if(count(platform) = 1, platform, 'both') AS platform FROM Spending GROUP BY 1, 2 ) SELECT - t1.*, + p.*, ifnull(sum(amount), 0) AS total_amount, - count(t2.user_id) AS total_users + count(t.user_id) AS total_users FROM - s AS t1 - LEFT JOIN t AS t2 USING (spend_date, platform) + P AS p + LEFT JOIN T AS t USING (spend_date, platform) GROUP BY 1, 2; diff --git a/solution/1100-1199/1132.Reported Posts II/README.md b/solution/1100-1199/1132.Reported Posts II/README.md index 3807f1a04ed3a..ee6685ba74e75 100644 --- a/solution/1100-1199/1132.Reported Posts II/README.md +++ b/solution/1100-1199/1132.Reported Posts II/README.md @@ -98,18 +98,20 @@ Removals table: ```sql # Write your MySQL query statement below -SELECT - round(avg(avg_per * 100), 2) AS average_daily_percent -FROM - ( +WITH + T AS ( SELECT - count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per + count(DISTINCT t2.post_id) / count( + DISTINCT t1.post_id + ) * 100 AS percent FROM Actions AS t1 LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id - WHERE t1.extra = 'spam' + WHERE extra = 'spam' GROUP BY action_date - ) AS t3; + ) +SELECT round(avg(percent), 2) AS average_daily_percent +FROM T; ``` diff --git a/solution/1100-1199/1132.Reported Posts II/README_EN.md b/solution/1100-1199/1132.Reported Posts II/README_EN.md index 238e4cd9d0da1..0e4754fd2e910 100644 --- a/solution/1100-1199/1132.Reported Posts II/README_EN.md +++ b/solution/1100-1199/1132.Reported Posts II/README_EN.md @@ -93,18 +93,20 @@ Note that the output is only one number and that we do not care about the remove ```sql # Write your MySQL query statement below -SELECT - round(avg(avg_per * 100), 2) AS average_daily_percent -FROM - ( +WITH + T AS ( SELECT - count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per + count(DISTINCT t2.post_id) / count( + DISTINCT t1.post_id + ) * 100 AS percent FROM Actions AS t1 LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id - WHERE t1.extra = 'spam' + WHERE extra = 'spam' GROUP BY action_date - ) AS t3; + ) +SELECT round(avg(percent), 2) AS average_daily_percent +FROM T; ``` diff --git a/solution/1100-1199/1132.Reported Posts II/Solution.sql b/solution/1100-1199/1132.Reported Posts II/Solution.sql index 90fbf8cf915a9..deccf0c9e7036 100644 --- a/solution/1100-1199/1132.Reported Posts II/Solution.sql +++ b/solution/1100-1199/1132.Reported Posts II/Solution.sql @@ -1,13 +1,15 @@ # Write your MySQL query statement below -SELECT - round(avg(avg_per * 100), 2) AS average_daily_percent -FROM - ( +WITH + T AS ( SELECT - count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per + count(DISTINCT t2.post_id) / count( + DISTINCT t1.post_id + ) * 100 AS percent FROM Actions AS t1 LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id - WHERE t1.extra = 'spam' + WHERE extra = 'spam' GROUP BY action_date - ) AS t3; + ) +SELECT round(avg(percent), 2) AS average_daily_percent +FROM T; diff --git a/solution/1100-1199/1141.User Activity for the Past 30 Days I/README.md b/solution/1100-1199/1141.User Activity for the Past 30 Days I/README.md index dd38e4db0e308..2c56706ed7277 100644 --- a/solution/1100-1199/1141.User Activity for the Past 30 Days I/README.md +++ b/solution/1100-1199/1141.User Activity for the Past 30 Days I/README.md @@ -66,19 +66,22 @@ Activity table: +**方法一:GROUP BY + HAVING** + +我们查询出所有在 `2019-07-27` 且在 $30$ 天内的所有活动记录,然后按照日期分组,统计每天的去重活跃用户数。 + ### **SQL** ```sql -SELECT - activity_date AS day, - COUNT(DISTINCT user_id) AS active_users +# Write your MySQL query statement below +SELECT activity_date AS day, count(DISTINCT user_id) AS active_users FROM Activity WHERE - DATEDIFF('2019-07-27', activity_date) >= 0 - AND DATEDIFF('2019-07-27', activity_date) < 30 -GROUP BY activity_date; + activity_date <= '2019-07-27' + AND datediff('2019-07-27', activity_date) < 30 +GROUP BY 1; ``` diff --git a/solution/1100-1199/1141.User Activity for the Past 30 Days I/README_EN.md b/solution/1100-1199/1141.User Activity for the Past 30 Days I/README_EN.md index ed93062ca63d7..2346e71e095d2 100644 --- a/solution/1100-1199/1141.User Activity for the Past 30 Days I/README_EN.md +++ b/solution/1100-1199/1141.User Activity for the Past 30 Days I/README_EN.md @@ -67,14 +67,13 @@ Activity table: ### **SQL** ```sql -SELECT - activity_date AS day, - COUNT(DISTINCT user_id) AS active_users +# Write your MySQL query statement below +SELECT activity_date AS day, count(DISTINCT user_id) AS active_users FROM Activity WHERE - DATEDIFF('2019-07-27', activity_date) >= 0 - AND DATEDIFF('2019-07-27', activity_date) < 30 -GROUP BY activity_date; + activity_date <= '2019-07-27' + AND datediff('2019-07-27', activity_date) < 30 +GROUP BY 1; ``` diff --git a/solution/1100-1199/1141.User Activity for the Past 30 Days I/Solution.sql b/solution/1100-1199/1141.User Activity for the Past 30 Days I/Solution.sql index 1df9e22b6168d..d2880d36d7d80 100644 --- a/solution/1100-1199/1141.User Activity for the Past 30 Days I/Solution.sql +++ b/solution/1100-1199/1141.User Activity for the Past 30 Days I/Solution.sql @@ -1,8 +1,7 @@ -SELECT - activity_date AS day, - COUNT(DISTINCT user_id) AS active_users +# Write your MySQL query statement below +SELECT activity_date AS day, count(DISTINCT user_id) AS active_users FROM Activity WHERE - DATEDIFF('2019-07-27', activity_date) >= 0 - AND DATEDIFF('2019-07-27', activity_date) < 30 -GROUP BY activity_date; + activity_date <= '2019-07-27' + AND datediff('2019-07-27', activity_date) < 30 +GROUP BY 1; diff --git a/solution/1100-1199/1142.User Activity for the Past 30 Days II/README.md b/solution/1100-1199/1142.User Activity for the Past 30 Days II/README.md index 98d4393241884..acf1cc4d37f7f 100644 --- a/solution/1100-1199/1142.User Activity for the Past 30 Days II/README.md +++ b/solution/1100-1199/1142.User Activity for the Past 30 Days II/README.md @@ -72,15 +72,18 @@ Activity 表: ```sql # Write your MySQL query statement below -SELECT - IFNULL( - ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2), - 0 - ) AS average_sessions_per_user -FROM Activity -WHERE - DATEDIFF('2019-07-27', activity_date) >= 0 - AND DATEDIFF('2019-07-27', activity_date) < 30; +WITH + T AS ( + SELECT + count(DISTINCT session_id) AS sessions + FROM Activity + WHERE + activity_date <= '2019-07-27' + AND datediff('2019-07-27', activity_date) < 30 + GROUP BY user_id + ) +SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user +FROM T; ``` diff --git a/solution/1100-1199/1142.User Activity for the Past 30 Days II/README_EN.md b/solution/1100-1199/1142.User Activity for the Past 30 Days II/README_EN.md index bd902626aa2ad..d4e78d5189b52 100644 --- a/solution/1100-1199/1142.User Activity for the Past 30 Days II/README_EN.md +++ b/solution/1100-1199/1142.User Activity for the Past 30 Days II/README_EN.md @@ -68,15 +68,18 @@ Activity table: ```sql # Write your MySQL query statement below -SELECT - IFNULL( - ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2), - 0 - ) AS average_sessions_per_user -FROM Activity -WHERE - DATEDIFF('2019-07-27', activity_date) >= 0 - AND DATEDIFF('2019-07-27', activity_date) < 30; +WITH + T AS ( + SELECT + count(DISTINCT session_id) AS sessions + FROM Activity + WHERE + activity_date <= '2019-07-27' + AND datediff('2019-07-27', activity_date) < 30 + GROUP BY user_id + ) +SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user +FROM T; ``` diff --git a/solution/1100-1199/1142.User Activity for the Past 30 Days II/Solution.sql b/solution/1100-1199/1142.User Activity for the Past 30 Days II/Solution.sql index f171d6352d451..14c0ee9338951 100644 --- a/solution/1100-1199/1142.User Activity for the Past 30 Days II/Solution.sql +++ b/solution/1100-1199/1142.User Activity for the Past 30 Days II/Solution.sql @@ -1,10 +1,13 @@ # Write your MySQL query statement below -SELECT - IFNULL( - ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2), - 0 - ) AS average_sessions_per_user -FROM Activity -WHERE - DATEDIFF('2019-07-27', activity_date) >= 0 - AND DATEDIFF('2019-07-27', activity_date) < 30; +WITH + T AS ( + SELECT + count(DISTINCT session_id) AS sessions + FROM Activity + WHERE + activity_date <= '2019-07-27' + AND datediff('2019-07-27', activity_date) < 30 + GROUP BY user_id + ) +SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user +FROM T; diff --git a/solution/1100-1199/1148.Article Views I/README.md b/solution/1100-1199/1148.Article Views I/README.md index 2c5cead168379..d8db86a2076af 100644 --- a/solution/1100-1199/1148.Article Views I/README.md +++ b/solution/1100-1199/1148.Article Views I/README.md @@ -55,17 +55,20 @@ Views 表: -“`DISTINCT` + `ORDER BY`” 实现。 +**方法一:DISTINCT + WHERE** + +我们利用 `WHERE` 子句来筛选出 `author_id` 和 `viewer_id` 相等的记录,然后利用 `DISTINCT` 来去重,最后按照 `id` 排序即可。 ### **SQL** ```sql -SELECT DISTINCT (author_id) AS id +# Write your MySQL query statement below +SELECT DISTINCT author_id AS id FROM Views WHERE author_id = viewer_id -ORDER BY id; +ORDER BY 1; ``` diff --git a/solution/1100-1199/1148.Article Views I/README_EN.md b/solution/1100-1199/1148.Article Views I/README_EN.md index 89898a3bf15d1..147033c783f5e 100644 --- a/solution/1100-1199/1148.Article Views I/README_EN.md +++ b/solution/1100-1199/1148.Article Views I/README_EN.md @@ -61,10 +61,11 @@ Views table: ### **SQL** ```sql -SELECT DISTINCT (author_id) AS id +# Write your MySQL query statement below +SELECT DISTINCT author_id AS id FROM Views WHERE author_id = viewer_id -ORDER BY id; +ORDER BY 1; ``` diff --git a/solution/1100-1199/1148.Article Views I/Solution.sql b/solution/1100-1199/1148.Article Views I/Solution.sql index 6c4c149059fe3..2a3cd6c02366a 100644 --- a/solution/1100-1199/1148.Article Views I/Solution.sql +++ b/solution/1100-1199/1148.Article Views I/Solution.sql @@ -1,4 +1,5 @@ -SELECT DISTINCT (author_id) AS id +# Write your MySQL query statement below +SELECT DISTINCT author_id AS id FROM Views WHERE author_id = viewer_id -ORDER BY id; +ORDER BY 1; diff --git a/solution/1100-1199/1149.Article Views II/README.md b/solution/1100-1199/1149.Article Views II/README.md index c9c6b74de1f6b..2390d8b57f362 100644 --- a/solution/1100-1199/1149.Article Views II/README.md +++ b/solution/1100-1199/1149.Article Views II/README.md @@ -61,7 +61,9 @@ Views 表: -“`DISTINCT` + `GROUP BY`” 实现。 +**方法一:DISTINCT + GROUP BY + HAVING** + +我们将数据按照 `viewer_id` 和 `view_date` 分组,然后利用 `HAVING` 子句来筛选出浏览文章数大于 $1$ 的记录,最后按照 `id` 去重排序即可。 @@ -69,12 +71,11 @@ Views 表: ```sql # Write your MySQL query statement below -SELECT DISTINCT - (viewer_id) AS id +SELECT DISTINCT viewer_id AS id FROM Views -GROUP BY view_date, viewer_id -HAVING COUNT(DISTINCT article_id) > 1 -ORDER BY id; +GROUP BY viewer_id, view_date +HAVING count(DISTINCT article_id) > 1 +ORDER BY 1; ``` diff --git a/solution/1100-1199/1149.Article Views II/README_EN.md b/solution/1100-1199/1149.Article Views II/README_EN.md index 368ea900e6f1d..382c2ab9fbc47 100644 --- a/solution/1100-1199/1149.Article Views II/README_EN.md +++ b/solution/1100-1199/1149.Article Views II/README_EN.md @@ -62,12 +62,11 @@ Views table: ```sql # Write your MySQL query statement below -SELECT DISTINCT - (viewer_id) AS id +SELECT DISTINCT viewer_id AS id FROM Views -GROUP BY view_date, viewer_id -HAVING COUNT(DISTINCT article_id) > 1 -ORDER BY id; +GROUP BY viewer_id, view_date +HAVING count(DISTINCT article_id) > 1 +ORDER BY 1; ``` diff --git a/solution/1100-1199/1149.Article Views II/Solution.sql b/solution/1100-1199/1149.Article Views II/Solution.sql index 6a3c820a27bbe..55402d92887bc 100644 --- a/solution/1100-1199/1149.Article Views II/Solution.sql +++ b/solution/1100-1199/1149.Article Views II/Solution.sql @@ -1,7 +1,6 @@ # Write your MySQL query statement below -SELECT DISTINCT - (viewer_id) AS id +SELECT DISTINCT viewer_id AS id FROM Views -GROUP BY view_date, viewer_id -HAVING COUNT(DISTINCT article_id) > 1 -ORDER BY id; +GROUP BY viewer_id, view_date +HAVING count(DISTINCT article_id) > 1 +ORDER BY 1; diff --git a/solution/1100-1199/1158.Market Analysis I/README.md b/solution/1100-1199/1158.Market Analysis I/README.md index 80f168bcd0802..68d33fa1a27ef 100644 --- a/solution/1100-1199/1158.Market Analysis I/README.md +++ b/solution/1100-1199/1158.Market Analysis I/README.md @@ -125,4 +125,16 @@ FROM GROUP BY user_id; ``` +```sql +# Write your MySQL query statement below +SELECT + user_id AS buyer_id, + join_date, + ifnull(sum(year(order_date) = 2019), 0) AS orders_in_2019 +FROM + Users AS u + LEFT JOIN Orders AS o ON u.user_id = buyer_id +GROUP BY 1; +``` + diff --git a/solution/1100-1199/1158.Market Analysis I/README_EN.md b/solution/1100-1199/1158.Market Analysis I/README_EN.md index 5b17a9f80d1a1..67b394b4a1c04 100644 --- a/solution/1100-1199/1158.Market Analysis I/README_EN.md +++ b/solution/1100-1199/1158.Market Analysis I/README_EN.md @@ -122,4 +122,16 @@ FROM GROUP BY user_id; ``` +```sql +# Write your MySQL query statement below +SELECT + user_id AS buyer_id, + join_date, + ifnull(sum(year(order_date) = 2019), 0) AS orders_in_2019 +FROM + Users AS u + LEFT JOIN Orders AS o ON u.user_id = buyer_id +GROUP BY 1; +``` + diff --git a/solution/1100-1199/1158.Market Analysis I/Solution.sql b/solution/1100-1199/1158.Market Analysis I/Solution.sql index 88358aea0c455..c350404e187e9 100644 --- a/solution/1100-1199/1158.Market Analysis I/Solution.sql +++ b/solution/1100-1199/1158.Market Analysis I/Solution.sql @@ -1,9 +1,9 @@ # Write your MySQL query statement below SELECT - u.user_id AS buyer_id, - u.join_date, - count(order_id) AS orders_in_2019 + user_id AS buyer_id, + join_date, + ifnull(sum(year(order_date) = 2019), 0) AS orders_in_2019 FROM Users AS u - LEFT JOIN Orders AS o ON u.user_id = o.buyer_id AND YEAR(order_date) = 2019 -GROUP BY user_id; + LEFT JOIN Orders AS o ON u.user_id = buyer_id +GROUP BY 1;