File tree 6 files changed +99
-4
lines changed
2142.The Number of Passengers in Each Bus I
2173.Longest Winning Streak
6 files changed +99
-4
lines changed Original file line number Diff line number Diff line change @@ -95,7 +95,17 @@ Passengers 表:
95
95
<!-- 这里可写当前语言的特殊实现逻辑 -->
96
96
97
97
``` sql
98
-
98
+ # Write your MySQL query statement below
99
+ SELECT
100
+ bus_id,
101
+ count (passenger_id) - lag(count (passenger_id), 1 , 0 ) OVER (
102
+ ORDER BY b .arrival_time
103
+ ) AS passengers_cnt
104
+ FROM
105
+ Buses AS b
106
+ LEFT JOIN Passengers AS p ON p .arrival_time <= b .arrival_time
107
+ GROUP BY 1
108
+ ORDER BY 1 ;
99
109
```
100
110
101
111
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -92,7 +92,17 @@ Passengers table:
92
92
### ** SQL**
93
93
94
94
``` sql
95
-
95
+ # Write your MySQL query statement below
96
+ SELECT
97
+ bus_id,
98
+ count (passenger_id) - lag(count (passenger_id), 1 , 0 ) OVER (
99
+ ORDER BY b .arrival_time
100
+ ) AS passengers_cnt
101
+ FROM
102
+ Buses AS b
103
+ LEFT JOIN Passengers AS p ON p .arrival_time <= b .arrival_time
104
+ GROUP BY 1
105
+ ORDER BY 1 ;
96
106
```
97
107
98
108
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ SELECT
3
+ bus_id,
4
+ count (passenger_id) - lag(count (passenger_id), 1 , 0 ) OVER (
5
+ ORDER BY b .arrival_time
6
+ ) AS passengers_cnt
7
+ FROM
8
+ Buses AS b
9
+ LEFT JOIN Passengers AS p ON p .arrival_time <= b .arrival_time
10
+ GROUP BY 1
11
+ ORDER BY 1 ;
Original file line number Diff line number Diff line change @@ -88,7 +88,28 @@ Player 3:
88
88
<!-- 这里可写当前语言的特殊实现逻辑 -->
89
89
90
90
``` sql
91
-
91
+ # Write your MySQL query statement below
92
+ WITH
93
+ S AS (
94
+ SELECT
95
+ * ,
96
+ row_number() OVER (
97
+ PARTITION BY player_id
98
+ ORDER BY match_day
99
+ ) - row_number() OVER (
100
+ PARTITION BY player_id, result
101
+ ORDER BY match_day
102
+ ) AS rk
103
+ FROM Matches
104
+ ),
105
+ T AS (
106
+ SELECT player_id, sum (result = ' Win' ) AS s
107
+ FROM S
108
+ GROUP BY player_id, rk
109
+ )
110
+ SELECT player_id, max (s) AS longest_streak
111
+ FROM T
112
+ GROUP BY player_id;
92
113
```
93
114
94
115
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -81,7 +81,28 @@ The longest winning streak was 1 match.
81
81
### ** SQL**
82
82
83
83
``` sql
84
-
84
+ # Write your MySQL query statement below
85
+ WITH
86
+ S AS (
87
+ SELECT
88
+ * ,
89
+ row_number() OVER (
90
+ PARTITION BY player_id
91
+ ORDER BY match_day
92
+ ) - row_number() OVER (
93
+ PARTITION BY player_id, result
94
+ ORDER BY match_day
95
+ ) AS rk
96
+ FROM Matches
97
+ ),
98
+ T AS (
99
+ SELECT player_id, sum (result = ' Win' ) AS s
100
+ FROM S
101
+ GROUP BY player_id, rk
102
+ )
103
+ SELECT player_id, max (s) AS longest_streak
104
+ FROM T
105
+ GROUP BY player_id;
85
106
```
86
107
87
108
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ WITH
3
+ S AS (
4
+ SELECT
5
+ * ,
6
+ row_number() OVER (
7
+ PARTITION BY player_id
8
+ ORDER BY match_day
9
+ ) - row_number() OVER (
10
+ PARTITION BY player_id, result
11
+ ORDER BY match_day
12
+ ) AS rk
13
+ FROM Matches
14
+ ),
15
+ T AS (
16
+ SELECT player_id, sum (result = ' Win' ) AS s
17
+ FROM S
18
+ GROUP BY player_id, rk
19
+ )
20
+ SELECT player_id, max (s) AS longest_streak
21
+ FROM T
22
+ GROUP BY player_id;
You can’t perform that action at this time.
0 commit comments