Skip to content

Commit eebad8a

Browse files
authored
feat: add sql solutions to lc problems (doocs#1199)
* No.1097.Game Play Analysis V * No.1098.Unpopular Books * No.1107.New Users Daily Count * No.1112.Highest Grade For Each Student
1 parent 3133ba5 commit eebad8a

File tree

12 files changed

+87
-110
lines changed

12 files changed

+87
-110
lines changed

solution/1000-1099/1097.Game Play Analysis V/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Result 表:
6868
```sql
6969
# Write your MySQL query statement below
7070
WITH
71-
t AS (
71+
T AS (
7272
SELECT
7373
player_id,
7474
event_date,
@@ -79,13 +79,11 @@ SELECT
7979
install_dt,
8080
count(DISTINCT player_id) AS installs,
8181
round(
82-
sum(if(datediff(event_date, install_dt) = 1, 1, 0)) / count(
83-
DISTINCT player_id
84-
),
82+
sum(datediff(event_date, install_dt) = 1) / count(DISTINCT player_id),
8583
2
8684
) AS day1_retention
87-
FROM t
88-
GROUP BY install_dt;
85+
FROM T
86+
GROUP BY 1;
8987
```
9088

9189
<!-- tabs:end -->

solution/1000-1099/1097.Game Play Analysis V/README_EN.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Player 2 installed the game on 2017-06-25 but didn&#39;t log back in on 2017-06-
6868
```sql
6969
# Write your MySQL query statement below
7070
WITH
71-
t AS (
71+
T AS (
7272
SELECT
7373
player_id,
7474
event_date,
@@ -79,13 +79,11 @@ SELECT
7979
install_dt,
8080
count(DISTINCT player_id) AS installs,
8181
round(
82-
sum(if(datediff(event_date, install_dt) = 1, 1, 0)) / count(
83-
DISTINCT player_id
84-
),
82+
sum(datediff(event_date, install_dt) = 1) / count(DISTINCT player_id),
8583
2
8684
) AS day1_retention
87-
FROM t
88-
GROUP BY install_dt;
85+
FROM T
86+
GROUP BY 1;
8987
```
9088

9189
<!-- tabs:end -->
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Write your MySQL query statement below
22
WITH
3-
t AS (
3+
T AS (
44
SELECT
55
player_id,
66
event_date,
@@ -11,10 +11,8 @@ SELECT
1111
install_dt,
1212
count(DISTINCT player_id) AS installs,
1313
round(
14-
sum(if(datediff(event_date, install_dt) = 1, 1, 0)) / count(
15-
DISTINCT player_id
16-
),
14+
sum(datediff(event_date, install_dt) = 1) / count(DISTINCT player_id),
1715
2
1816
) AS day1_retention
19-
FROM t
20-
GROUP BY install_dt;
17+
FROM T
18+
GROUP BY 1;

solution/1000-1099/1098.Unpopular Books/README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,13 @@ Result 表:
8686

8787
```sql
8888
# Write your MySQL query statement below
89-
SELECT
90-
b.book_id,
91-
b.NAME
89+
SELECT book_id, name
9290
FROM
93-
books b
94-
LEFT JOIN orders o ON b.book_id = o.book_id
95-
WHERE
96-
b.available_from < '2019-05-23'
97-
GROUP BY
98-
b.book_id
99-
HAVING
100-
ifnull( sum( IF ( o.dispatch_date < '2018-06-23', 0, quantity )), 0 )< 10
101-
ORDER BY
102-
b.book_id
91+
Books
92+
LEFT JOIN Orders USING (book_id)
93+
WHERE available_from < '2019-05-23'
94+
GROUP BY 1
95+
HAVING sum(if(dispatch_date >= '2018-06-23', quantity, 0)) < 10;
10396
```
10497

10598
<!-- tabs:end -->

solution/1000-1099/1098.Unpopular Books/README_EN.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,13 @@ Orders table:
8787

8888
```sql
8989
# Write your MySQL query statement below
90-
SELECT
91-
b.book_id,
92-
b.NAME
90+
SELECT book_id, name
9391
FROM
94-
books b
95-
LEFT JOIN orders o ON b.book_id = o.book_id
96-
WHERE
97-
b.available_from < '2019-05-23'
98-
GROUP BY
99-
b.book_id
100-
HAVING
101-
ifnull( sum( IF ( o.dispatch_date < '2018-06-23', 0, quantity )), 0 )< 10
102-
ORDER BY
103-
b.book_id
92+
Books
93+
LEFT JOIN Orders USING (book_id)
94+
WHERE available_from < '2019-05-23'
95+
GROUP BY 1
96+
HAVING sum(if(dispatch_date >= '2018-06-23', quantity, 0)) < 10;
10497
```
10598

10699
<!-- tabs:end -->
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
b.book_id,
4-
b.NAME
2+
SELECT book_id, name
53
FROM
6-
books AS b
7-
LEFT JOIN orders AS o ON b.book_id = o.book_id
8-
WHERE b.available_from < '2019-05-23'
9-
GROUP BY b.book_id
10-
HAVING ifnull(sum(IF(o.dispatch_date < '2018-06-23', 0, quantity)), 0) < 10
11-
ORDER BY b.book_id;
4+
Books
5+
LEFT JOIN Orders USING (book_id)
6+
WHERE available_from < '2019-05-23'
7+
GROUP BY 1
8+
HAVING sum(if(dispatch_date >= '2018-06-23', quantity, 0)) < 10;

solution/1100-1199/1107.New Users Daily Count/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ ID 为 5 的用户第一次登陆于 2019-03-01,因此他不算在 2019-06-21
6767

6868
```sql
6969
# Write your MySQL query statement below
70-
SELECT
71-
login_date,
72-
count(user_id) AS user_count
73-
FROM
74-
(
75-
SELECT min(activity_date) AS login_date, user_id
70+
WITH
71+
T AS (
72+
SELECT
73+
user_id,
74+
min(activity_date) OVER (PARTITION BY user_id) AS login_date
7675
FROM Traffic
7776
WHERE activity = 'login'
78-
GROUP BY user_id
79-
) AS t
80-
WHERE DATEDIFF('2019-6-30', login_date) <= 90
81-
GROUP BY login_date;
77+
)
78+
SELECT login_date, count(DISTINCT user_id) AS user_count
79+
FROM T
80+
WHERE datediff('2019-06-30', login_date) <= 90
81+
GROUP BY 1;
8282
```
8383

8484
<!-- tabs:end -->

solution/1100-1199/1107.New Users Daily Count/README_EN.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ The user with id 5 first logged in on 2019-03-01 so he&#39;s not counted on 2019
7171

7272
```sql
7373
# Write your MySQL query statement below
74-
SELECT
75-
login_date,
76-
count(user_id) AS user_count
77-
FROM
78-
(
79-
SELECT min(activity_date) AS login_date, user_id
74+
WITH
75+
T AS (
76+
SELECT
77+
user_id,
78+
min(activity_date) OVER (PARTITION BY user_id) AS login_date
8079
FROM Traffic
8180
WHERE activity = 'login'
82-
GROUP BY user_id
83-
) AS t
84-
WHERE DATEDIFF('2019-6-30', login_date) <= 90
85-
GROUP BY login_date;
81+
)
82+
SELECT login_date, count(DISTINCT user_id) AS user_count
83+
FROM T
84+
WHERE datediff('2019-06-30', login_date) <= 90
85+
GROUP BY 1;
8686
```
8787

8888
<!-- tabs:end -->
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
login_date,
4-
count(user_id) AS user_count
5-
FROM
6-
(
7-
SELECT min(activity_date) AS login_date, user_id
2+
WITH
3+
T AS (
4+
SELECT
5+
user_id,
6+
min(activity_date) OVER (PARTITION BY user_id) AS login_date
87
FROM Traffic
98
WHERE activity = 'login'
10-
GROUP BY user_id
11-
) AS t
12-
WHERE DATEDIFF('2019-6-30', login_date) <= 90
13-
GROUP BY login_date;
9+
)
10+
SELECT login_date, count(DISTINCT user_id) AS user_count
11+
FROM T
12+
WHERE datediff('2019-06-30', login_date) <= 90
13+
GROUP BY 1;

solution/1100-1199/1112.Highest Grade For Each Student/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ Enrollments 表:
6363
### **SQL**
6464

6565
```sql
66-
SELECT
67-
student_id,
68-
course_id,
69-
grade
70-
FROM
71-
(
66+
# Write your MySQL query statement below
67+
WITH
68+
T AS (
7269
SELECT
7370
*,
74-
RANK() OVER (
71+
rank() OVER (
7572
PARTITION BY student_id
7673
ORDER BY grade DESC, course_id
7774
) AS rk
7875
FROM Enrollments
79-
) AS a
80-
WHERE a.rk = 1;
76+
)
77+
SELECT student_id, course_id, grade
78+
FROM T
79+
WHERE rk = 1
80+
ORDER BY student_id;
8181
```
8282

8383
<!-- tabs:end -->

0 commit comments

Comments
 (0)