File tree Expand file tree Collapse file tree 12 files changed +184
-8
lines changed
2100-2199/2159.Order Two Columns Independently
2228.Users With Two Purchases Within Seven Days
2298.Tasks Count in the Weekend
2300-2399/2308.Arrange Table by Gender Expand file tree Collapse file tree 12 files changed +184
-8
lines changed Original file line number Diff line number Diff line change @@ -66,7 +66,24 @@ Data 表:
66
66
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
67
68
68
``` sql
69
-
69
+ # Write your MySQL query statement below
70
+ WITH
71
+ S AS (
72
+ SELECT
73
+ first_col,
74
+ row_number() OVER (ORDER BY first_col) AS rk
75
+ FROM Data
76
+ ),
77
+ T AS (
78
+ SELECT
79
+ second_col,
80
+ row_number() OVER (ORDER BY second_col DESC ) AS rk
81
+ FROM Data
82
+ )
83
+ SELECT first_col, second_col
84
+ FROM
85
+ S
86
+ JOIN T USING (rk);
70
87
```
71
88
72
89
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -59,7 +59,24 @@ Data table:
59
59
### ** SQL**
60
60
61
61
``` sql
62
-
62
+ # Write your MySQL query statement below
63
+ WITH
64
+ S AS (
65
+ SELECT
66
+ first_col,
67
+ row_number() OVER (ORDER BY first_col) AS rk
68
+ FROM Data
69
+ ),
70
+ T AS (
71
+ SELECT
72
+ second_col,
73
+ row_number() OVER (ORDER BY second_col DESC ) AS rk
74
+ FROM Data
75
+ )
76
+ SELECT first_col, second_col
77
+ FROM
78
+ S
79
+ JOIN T USING (rk);
63
80
```
64
81
65
82
<!-- 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
+ first_col,
6
+ row_number() OVER (ORDER BY first_col) AS rk
7
+ FROM Data
8
+ ),
9
+ T AS (
10
+ SELECT
11
+ second_col,
12
+ row_number() OVER (ORDER BY second_col DESC ) AS rk
13
+ FROM Data
14
+ )
15
+ SELECT first_col, second_col
16
+ FROM
17
+ S
18
+ JOIN T USING (rk);
Original file line number Diff line number Diff line change @@ -68,7 +68,24 @@ Purchases 表:
68
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
69
70
70
``` sql
71
-
71
+ # Write your MySQL query statement below
72
+ WITH
73
+ t AS (
74
+ SELECT
75
+ user_id,
76
+ datediff(
77
+ purchase_date,
78
+ lag(purchase_date, 1 ) OVER (
79
+ PARTITION BY user_id
80
+ ORDER BY purchase_date
81
+ )
82
+ ) AS d
83
+ FROM Purchases
84
+ )
85
+ SELECT DISTINCT user_id
86
+ FROM t
87
+ WHERE d <= 7
88
+ ORDER BY user_id;
72
89
```
73
90
74
91
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -62,7 +62,24 @@ User 7 had two purchases on the same day so we add their ID.
62
62
### ** SQL**
63
63
64
64
``` sql
65
-
65
+ # Write your MySQL query statement below
66
+ WITH
67
+ t AS (
68
+ SELECT
69
+ user_id,
70
+ datediff(
71
+ purchase_date,
72
+ lag(purchase_date, 1 ) OVER (
73
+ PARTITION BY user_id
74
+ ORDER BY purchase_date
75
+ )
76
+ ) AS d
77
+ FROM Purchases
78
+ )
79
+ SELECT DISTINCT user_id
80
+ FROM t
81
+ WHERE d <= 7
82
+ ORDER BY user_id;
66
83
```
67
84
68
85
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ WITH
3
+ t AS (
4
+ SELECT
5
+ user_id,
6
+ datediff(
7
+ purchase_date,
8
+ lag(purchase_date, 1 ) OVER (
9
+ PARTITION BY user_id
10
+ ORDER BY purchase_date
11
+ )
12
+ ) AS d
13
+ FROM Purchases
14
+ )
15
+ SELECT DISTINCT user_id
16
+ FROM t
17
+ WHERE d <= 7
18
+ ORDER BY user_id;
Original file line number Diff line number Diff line change @@ -70,14 +70,22 @@ Task 6 是在周日提交的。
70
70
71
71
<!-- 这里可写通用的实现逻辑 -->
72
72
73
+ ** 方法一:WEEKDAY() 函数**
74
+
75
+ ` WEEKDAY() ` 函数返回日期的工作日编号,从 0 开始,0 表示星期一,1 表示星期二,以此类推,6 表示星期日。
76
+
73
77
<!-- tabs:start -->
74
78
75
79
### ** SQL**
76
80
77
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
78
82
79
83
``` sql
80
-
84
+ # Write your MySQL query statement below
85
+ SELECT
86
+ sum (weekday(submit_date) IN (5 , 6 )) AS weekend_cnt,
87
+ sum (weekday(submit_date) NOT IN (5 , 6 )) AS working_cnt
88
+ FROM Tasks;
81
89
```
82
90
83
91
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -71,7 +71,11 @@ Task 6 was submitted on Sunday.
71
71
### ** SQL**
72
72
73
73
``` sql
74
-
74
+ # Write your MySQL query statement below
75
+ SELECT
76
+ sum (weekday(submit_date) IN (5 , 6 )) AS weekend_cnt,
77
+ sum (weekday(submit_date) NOT IN (5 , 6 )) AS working_cnt
78
+ FROM Tasks;
75
79
```
76
80
77
81
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ SELECT
3
+ sum (weekday(submit_date) IN (5 , 6 )) AS weekend_cnt,
4
+ sum (weekday(submit_date) NOT IN (5 , 6 )) AS working_cnt
5
+ FROM Tasks;
Original file line number Diff line number Diff line change @@ -80,7 +80,25 @@ Genders 表:
80
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
81
81
82
82
``` sql
83
-
83
+ # Write your MySQL query statement below
84
+ WITH
85
+ t AS (
86
+ SELECT
87
+ * ,
88
+ rank() OVER (
89
+ PARTITION BY gender
90
+ ORDER BY user_id
91
+ ) AS rk1,
92
+ CASE
93
+ WHEN gender = ' female' THEN 0
94
+ WHEN gender = ' other' THEN 1
95
+ ELSE 2
96
+ END AS rk2
97
+ FROM Genders
98
+ )
99
+ SELECT user_id, gender
100
+ FROM t
101
+ ORDER BY rk1, rk2;
84
102
```
85
103
86
104
<!-- tabs:end -->
You can’t perform that action at this time.
0 commit comments