Skip to content

Commit 8117658

Browse files
committed
feat: add solutions to lc problems: No.1501,1532
* No.1501.Countries You Can Safely Invest In * No.1532.The Most Recent Three Orders
1 parent 31bc918 commit 8117658

File tree

6 files changed

+152
-4
lines changed

6 files changed

+152
-4
lines changed

solution/1500-1599/1501.Countries You Can Safely Invest In/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,29 @@ Result 表:
120120
### **SQL**
121121

122122
```sql
123-
123+
# Write your MySQL query statement below
124+
with t as (
125+
select
126+
left(phone_number, 3) as country_code,
127+
avg(duration) as duration
128+
from
129+
Person
130+
join Calls on id in (caller_id, callee_id)
131+
group by
132+
country_code
133+
)
134+
select
135+
c.name country
136+
from
137+
Country c
138+
join t on c.country_code = t.country_code
139+
where
140+
t.duration > (
141+
select
142+
avg(duration)
143+
from
144+
Calls
145+
)
124146
```
125147

126148
<!-- tabs:end -->

solution/1500-1599/1501.Countries You Can Safely Invest In/README_EN.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,29 @@ Since Peru is the only country where the average call duration is greater than t
122122
### **SQL**
123123

124124
```sql
125-
125+
# Write your MySQL query statement below
126+
with t as (
127+
select
128+
left(phone_number, 3) as country_code,
129+
avg(duration) as duration
130+
from
131+
Person
132+
join Calls on id in (caller_id, callee_id)
133+
group by
134+
country_code
135+
)
136+
select
137+
c.name country
138+
from
139+
Country c
140+
join t on c.country_code = t.country_code
141+
where
142+
t.duration > (
143+
select
144+
avg(duration)
145+
from
146+
Calls
147+
)
126148
```
127149

128150
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Write your MySQL query statement below
2+
with t as (
3+
select
4+
left(phone_number, 3) as country_code,
5+
avg(duration) as duration
6+
from
7+
Person
8+
join Calls on id in (caller_id, callee_id)
9+
group by
10+
country_code
11+
)
12+
select
13+
c.name country
14+
from
15+
Country c
16+
join t on c.country_code = t.country_code
17+
where
18+
t.duration > (
19+
select
20+
avg(duration)
21+
from
22+
Calls
23+
)

solution/1500-1599/1532.The Most Recent Three Orders/README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,40 @@ Marwan 只有 1 笔订单。
106106

107107
<!-- 这里可写通用的实现逻辑 -->
108108

109+
**方法一:窗口函数**
110+
109111
<!-- tabs:start -->
110112

111113
### **SQL**
112114

113115
```sql
114-
116+
# Write your MySQL query statement below
117+
select
118+
name as customer_name,
119+
o.customer_id,
120+
order_id,
121+
order_date
122+
from
123+
Customers c
124+
join (
125+
select
126+
customer_id,
127+
order_date,
128+
order_id,
129+
rank() over(
130+
partition by customer_id
131+
order by
132+
order_date desc
133+
) rk
134+
from
135+
orders
136+
) o on c.customer_id = o.customer_id
137+
where
138+
rk <= 3
139+
order by
140+
name,
141+
o.customer_id,
142+
order_date desc;
115143
```
116144

117145
<!-- tabs:end -->

solution/1500-1599/1532.The Most Recent Three Orders/README_EN.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,33 @@ We sort the result table by customer_name in ascending order, by customer_id in
105105
### **SQL**
106106

107107
```sql
108-
108+
# Write your MySQL query statement below
109+
select
110+
name as customer_name,
111+
o.customer_id,
112+
order_id,
113+
order_date
114+
from
115+
Customers c
116+
join (
117+
select
118+
customer_id,
119+
order_date,
120+
order_id,
121+
rank() over(
122+
partition by customer_id
123+
order by
124+
order_date desc
125+
) rk
126+
from
127+
orders
128+
) o on c.customer_id = o.customer_id
129+
where
130+
rk <= 3
131+
order by
132+
name,
133+
o.customer_id,
134+
order_date desc;
109135
```
110136

111137
<!-- tabs:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Write your MySQL query statement below
2+
select
3+
name as customer_name,
4+
o.customer_id,
5+
order_id,
6+
order_date
7+
from
8+
Customers c
9+
join (
10+
select
11+
customer_id,
12+
order_date,
13+
order_id,
14+
rank() over(
15+
partition by customer_id
16+
order by
17+
order_date desc
18+
) rk
19+
from
20+
orders
21+
) o on c.customer_id = o.customer_id
22+
where
23+
rk <= 3
24+
order by
25+
name,
26+
o.customer_id,
27+
order_date desc;

0 commit comments

Comments
 (0)