Skip to content

Commit 1c50710

Browse files
authored
feat: add sql solutions to lc problems (doocs#1201)
* No.1127.User Purchase Platform * No.1132.Reported Posts II * No.1141.User Activity for the Past 30 Days I * No.1142.User Activity for the Past 30 Days II * No.1148.Article Views I * No.1149.Article Views II * No.1158.Market Analysis I
1 parent d40cc4e commit 1c50710

File tree

21 files changed

+168
-124
lines changed

21 files changed

+168
-124
lines changed

solution/1100-1199/1127.User Purchase Platform/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,29 @@ Result table:
6565
```sql
6666
# Write your MySQL query statement below
6767
WITH
68-
s AS (
68+
P AS (
6969
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
7070
UNION
71-
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
71+
SELECT DISTINCT spend_date, 'mobile' FROM Spending
7272
UNION
73-
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
73+
SELECT DISTINCT spend_date, 'both' FROM Spending
7474
),
75-
t AS (
75+
T AS (
7676
SELECT
7777
user_id,
7878
spend_date,
79-
if(count(platform) = 2, 'both', platform) AS platform,
80-
sum(amount) AS amount
79+
sum(amount) AS amount,
80+
if(count(platform) = 1, platform, 'both') AS platform
8181
FROM Spending
8282
GROUP BY 1, 2
8383
)
8484
SELECT
85-
t1.*,
85+
p.*,
8686
ifnull(sum(amount), 0) AS total_amount,
87-
count(t2.user_id) AS total_users
87+
count(t.user_id) AS total_users
8888
FROM
89-
s AS t1
90-
LEFT JOIN t AS t2 USING (spend_date, platform)
89+
P AS p
90+
LEFT JOIN T AS t USING (spend_date, platform)
9191
GROUP BY 1, 2;
9292
```
9393

solution/1100-1199/1127.User Purchase Platform/README_EN.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,29 @@ On 2019-07-02, user 2 purchased using mobile <strong>only</strong>, user 3 purch
6969
```sql
7070
# Write your MySQL query statement below
7171
WITH
72-
s AS (
72+
P AS (
7373
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
7474
UNION
75-
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
75+
SELECT DISTINCT spend_date, 'mobile' FROM Spending
7676
UNION
77-
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
77+
SELECT DISTINCT spend_date, 'both' FROM Spending
7878
),
79-
t AS (
79+
T AS (
8080
SELECT
8181
user_id,
8282
spend_date,
83-
if(count(platform) = 2, 'both', platform) AS platform,
84-
sum(amount) AS amount
83+
sum(amount) AS amount,
84+
if(count(platform) = 1, platform, 'both') AS platform
8585
FROM Spending
8686
GROUP BY 1, 2
8787
)
8888
SELECT
89-
t1.*,
89+
p.*,
9090
ifnull(sum(amount), 0) AS total_amount,
91-
count(t2.user_id) AS total_users
91+
count(t.user_id) AS total_users
9292
FROM
93-
s AS t1
94-
LEFT JOIN t AS t2 USING (spend_date, platform)
93+
P AS p
94+
LEFT JOIN T AS t USING (spend_date, platform)
9595
GROUP BY 1, 2;
9696
```
9797

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
# Write your MySQL query statement below
22
WITH
3-
s AS (
3+
P AS (
44
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
55
UNION
6-
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
6+
SELECT DISTINCT spend_date, 'mobile' FROM Spending
77
UNION
8-
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
8+
SELECT DISTINCT spend_date, 'both' FROM Spending
99
),
10-
t AS (
10+
T AS (
1111
SELECT
1212
user_id,
1313
spend_date,
14-
if(count(platform) = 2, 'both', platform) AS platform,
15-
sum(amount) AS amount
14+
sum(amount) AS amount,
15+
if(count(platform) = 1, platform, 'both') AS platform
1616
FROM Spending
1717
GROUP BY 1, 2
1818
)
1919
SELECT
20-
t1.*,
20+
p.*,
2121
ifnull(sum(amount), 0) AS total_amount,
22-
count(t2.user_id) AS total_users
22+
count(t.user_id) AS total_users
2323
FROM
24-
s AS t1
25-
LEFT JOIN t AS t2 USING (spend_date, platform)
24+
P AS p
25+
LEFT JOIN T AS t USING (spend_date, platform)
2626
GROUP BY 1, 2;

solution/1100-1199/1132.Reported Posts II/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,20 @@ Removals table:
9898

9999
```sql
100100
# Write your MySQL query statement below
101-
SELECT
102-
round(avg(avg_per * 100), 2) AS average_daily_percent
103-
FROM
104-
(
101+
WITH
102+
T AS (
105103
SELECT
106-
count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per
104+
count(DISTINCT t2.post_id) / count(
105+
DISTINCT t1.post_id
106+
) * 100 AS percent
107107
FROM
108108
Actions AS t1
109109
LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
110-
WHERE t1.extra = 'spam'
110+
WHERE extra = 'spam'
111111
GROUP BY action_date
112-
) AS t3;
112+
)
113+
SELECT round(avg(percent), 2) AS average_daily_percent
114+
FROM T;
113115
```
114116

115117
<!-- tabs:end -->

solution/1100-1199/1132.Reported Posts II/README_EN.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,20 @@ Note that the output is only one number and that we do not care about the remove
9393

9494
```sql
9595
# Write your MySQL query statement below
96-
SELECT
97-
round(avg(avg_per * 100), 2) AS average_daily_percent
98-
FROM
99-
(
96+
WITH
97+
T AS (
10098
SELECT
101-
count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per
99+
count(DISTINCT t2.post_id) / count(
100+
DISTINCT t1.post_id
101+
) * 100 AS percent
102102
FROM
103103
Actions AS t1
104104
LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
105-
WHERE t1.extra = 'spam'
105+
WHERE extra = 'spam'
106106
GROUP BY action_date
107-
) AS t3;
107+
)
108+
SELECT round(avg(percent), 2) AS average_daily_percent
109+
FROM T;
108110
```
109111

110112
<!-- tabs:end -->
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
round(avg(avg_per * 100), 2) AS average_daily_percent
4-
FROM
5-
(
2+
WITH
3+
T AS (
64
SELECT
7-
count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per
5+
count(DISTINCT t2.post_id) / count(
6+
DISTINCT t1.post_id
7+
) * 100 AS percent
88
FROM
99
Actions AS t1
1010
LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
11-
WHERE t1.extra = 'spam'
11+
WHERE extra = 'spam'
1212
GROUP BY action_date
13-
) AS t3;
13+
)
14+
SELECT round(avg(percent), 2) AS average_daily_percent
15+
FROM T;

solution/1100-1199/1141.User Activity for the Past 30 Days I/README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,22 @@ Activity table:
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:GROUP BY + HAVING**
70+
71+
我们查询出所有在 `2019-07-27` 且在 $30$ 天内的所有活动记录,然后按照日期分组,统计每天的去重活跃用户数。
72+
6973
<!-- tabs:start -->
7074

7175
### **SQL**
7276

7377
```sql
74-
SELECT
75-
activity_date AS day,
76-
COUNT(DISTINCT user_id) AS active_users
78+
# Write your MySQL query statement below
79+
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users
7780
FROM Activity
7881
WHERE
79-
DATEDIFF('2019-07-27', activity_date) >= 0
80-
AND DATEDIFF('2019-07-27', activity_date) < 30
81-
GROUP BY activity_date;
82+
activity_date <= '2019-07-27'
83+
AND datediff('2019-07-27', activity_date) < 30
84+
GROUP BY 1;
8285
```
8386

8487
<!-- tabs:end -->

solution/1100-1199/1141.User Activity for the Past 30 Days I/README_EN.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@ Activity table:
6767
### **SQL**
6868

6969
```sql
70-
SELECT
71-
activity_date AS day,
72-
COUNT(DISTINCT user_id) AS active_users
70+
# Write your MySQL query statement below
71+
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users
7372
FROM Activity
7473
WHERE
75-
DATEDIFF('2019-07-27', activity_date) >= 0
76-
AND DATEDIFF('2019-07-27', activity_date) < 30
77-
GROUP BY activity_date;
74+
activity_date <= '2019-07-27'
75+
AND datediff('2019-07-27', activity_date) < 30
76+
GROUP BY 1;
7877
```
7978

8079
<!-- tabs:end -->
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
SELECT
2-
activity_date AS day,
3-
COUNT(DISTINCT user_id) AS active_users
1+
# Write your MySQL query statement below
2+
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users
43
FROM Activity
54
WHERE
6-
DATEDIFF('2019-07-27', activity_date) >= 0
7-
AND DATEDIFF('2019-07-27', activity_date) < 30
8-
GROUP BY activity_date;
5+
activity_date <= '2019-07-27'
6+
AND datediff('2019-07-27', activity_date) < 30
7+
GROUP BY 1;

solution/1100-1199/1142.User Activity for the Past 30 Days II/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ Activity 表:
7272

7373
```sql
7474
# Write your MySQL query statement below
75-
SELECT
76-
IFNULL(
77-
ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2),
78-
0
79-
) AS average_sessions_per_user
80-
FROM Activity
81-
WHERE
82-
DATEDIFF('2019-07-27', activity_date) >= 0
83-
AND DATEDIFF('2019-07-27', activity_date) < 30;
75+
WITH
76+
T AS (
77+
SELECT
78+
count(DISTINCT session_id) AS sessions
79+
FROM Activity
80+
WHERE
81+
activity_date <= '2019-07-27'
82+
AND datediff('2019-07-27', activity_date) < 30
83+
GROUP BY user_id
84+
)
85+
SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user
86+
FROM T;
8487
```
8588

8689
<!-- tabs:end -->

0 commit comments

Comments
 (0)