Skip to content

Commit 71b395b

Browse files
authored
feat: add solutions to lc problems: No.1174,1193,1211,1633 (#1837)
* No.1174.Immediate Food Delivery II * No.1193.Monthly Transactions I * No.1211.Queries Quality and Percentage * No.1633.Percentage of Users Attended a Contest
1 parent b4dac32 commit 71b395b

File tree

11 files changed

+113
-71
lines changed

11 files changed

+113
-71
lines changed

solution/1100-1199/1174.Immediate Food Delivery II/README.md

+35-23
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,47 @@ Delivery 表:
6767

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

70+
**方法一:子查询**
71+
72+
我们可以使用子查询,先找到每个用户的首次订单,然后再计算即时订单的比例。
73+
74+
**方法二:窗口函数**
75+
76+
我们可以使用 `RANK()` 窗口函数,按照每个用户的订单日期升序排列,获取到每个用户的订单排名,然后我们筛选出排名为 $1$ 的订单,即为首次订单,再计算即时订单的比例。
77+
7078
<!-- tabs:start -->
7179

7280
### **SQL**
7381

7482
```sql
7583
# Write your MySQL query statement below
76-
select
77-
ROUND(
78-
SUM(
79-
IF(
80-
t1.order_date = t1.customer_pref_delivery_date,
81-
1,
82-
0
83-
)
84-
) / COUNT(1) * 100,
85-
2
86-
) as immediate_percentage
87-
from
88-
Delivery t1
89-
right join (
90-
select
91-
customer_id,
92-
MIN(order_date) as order_date
93-
from
94-
Delivery
95-
group by
96-
customer_id
97-
) t2 on t1.customer_id = t2.customer_id
98-
and t1.order_date = t2.order_date
84+
SELECT
85+
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
86+
FROM Delivery
87+
WHERE
88+
(customer_id, order_date) IN (
89+
SELECT customer_id, MIN(order_date)
90+
FROM Delivery
91+
GROUP BY 1
92+
);
93+
```
94+
95+
```sql
96+
# Write your MySQL query statement below
97+
WITH
98+
T AS (
99+
SELECT
100+
*,
101+
RANK() OVER (
102+
PARTITION BY customer_id
103+
ORDER BY order_date
104+
) AS rk
105+
FROM Delivery
106+
)
107+
SELECT
108+
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
109+
FROM T
110+
WHERE rk = 1;
99111
```
100112

101113
<!-- tabs:end -->

solution/1100-1199/1174.Immediate Food Delivery II/README_EN.md

+35-23
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,47 @@ Hence, half the customers have immediate first orders.
6262

6363
## Solutions
6464

65+
**Solution 1: Subquery**
66+
67+
We can use a subquery to first find the first order of each user, and then calculate the proportion of instant orders.
68+
69+
**Solution 2: Window Function**
70+
71+
We can use the `RANK()` window function to rank the orders of each user in ascending order by order date, and then filter out the orders with a rank of $1$, which are the first orders of each user. After that, we can calculate the proportion of instant orders.
72+
6573
<!-- tabs:start -->
6674

6775
### **SQL**
6876

6977
```sql
7078
# Write your MySQL query statement below
71-
select
72-
ROUND(
73-
SUM(
74-
IF(
75-
t1.order_date = t1.customer_pref_delivery_date,
76-
1,
77-
0
78-
)
79-
) / COUNT(1) * 100,
80-
2
81-
) as immediate_percentage
82-
from
83-
Delivery t1
84-
right join (
85-
select
86-
customer_id,
87-
MIN(order_date) as order_date
88-
from
89-
Delivery
90-
group by
91-
customer_id
92-
) t2 on t1.customer_id = t2.customer_id
93-
and t1.order_date = t2.order_date
79+
SELECT
80+
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
81+
FROM Delivery
82+
WHERE
83+
(customer_id, order_date) IN (
84+
SELECT customer_id, MIN(order_date)
85+
FROM Delivery
86+
GROUP BY 1
87+
);
88+
```
89+
90+
```sql
91+
# Write your MySQL query statement below
92+
WITH
93+
T AS (
94+
SELECT
95+
*,
96+
RANK() OVER (
97+
PARTITION BY customer_id
98+
ORDER BY order_date
99+
) AS rk
100+
FROM Delivery
101+
)
102+
SELECT
103+
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
104+
FROM T
105+
WHERE rk = 1;
94106
```
95107

96108
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# Write your MySQL query statement below
22
SELECT
3-
ROUND(
4-
SUM(IF(t1.order_date = t1.customer_pref_delivery_date, 1, 0)) / COUNT(1) * 100,
5-
2
6-
) AS immediate_percentage
7-
FROM
8-
Delivery AS t1
9-
RIGHT JOIN (
10-
SELECT
11-
customer_id,
12-
MIN(order_date) AS order_date
3+
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
4+
FROM Delivery
5+
WHERE
6+
(customer_id, order_date) IN (
7+
SELECT customer_id, MIN(order_date)
138
FROM Delivery
14-
GROUP BY customer_id
15-
) AS t2
16-
ON t1.customer_id = t2.customer_id AND t1.order_date = t2.order_date;
9+
GROUP BY 1
10+
);

solution/1100-1199/1193.Monthly Transactions I/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Transactions</code> table:
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:分组求和**
63+
64+
我们可以先按照月份和国家分组,然后利用 `COUNT``SUM` 函数分别求出每个分组的事务数、已批准的事务数、总金额和已批准的总金额。
65+
6266
<!-- tabs:start -->
6367

6468
### **SQL**

solution/1100-1199/1193.Monthly Transactions I/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Transactions table:
5555

5656
## Solutions
5757

58+
**Solution 1: Grouping and Aggregation**
59+
60+
We can first group by month and country, and then use the `COUNT` and `SUM` functions to respectively calculate the number of transactions, the number of approved transactions, the total amount, and the total amount of approved transactions for each group.
61+
5862
<!-- tabs:start -->
5963

6064
### **SQL**

solution/1200-1299/1211.Queries Quality and Percentage/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33
8181

8282
<!-- 这里可写通用的实现逻辑 -->
8383

84+
**方法一:分组统计**
85+
86+
我们将查询结果按照 `query_name` 进行分组,然后利用 `AVG``ROUND` 函数计算 `quality``poor_query_percentage`
87+
8488
<!-- tabs:start -->
8589

8690
### **SQL**
@@ -90,9 +94,9 @@ Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33
9094
SELECT
9195
query_name,
9296
ROUND(AVG(rating / position), 2) AS quality,
93-
ROUND(100 * AVG(rating < 3), 2) AS poor_query_percentage
97+
ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
9498
FROM Queries
95-
GROUP BY query_name;
99+
GROUP BY 1;
96100
```
97101

98102
<!-- tabs:end -->

solution/1200-1299/1211.Queries Quality and Percentage/README_EN.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
7676

7777
## Solutions
7878

79+
**Solution 1: Grouping and Aggregation**
80+
81+
We can group the query results by `query_name`, and then use the `AVG` and `ROUND` functions to calculate `quality` and `poor_query_percentage`.
82+
7983
<!-- tabs:start -->
8084

8185
### **SQL**
@@ -85,9 +89,9 @@ Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
8589
SELECT
8690
query_name,
8791
ROUND(AVG(rating / position), 2) AS quality,
88-
ROUND(100 * AVG(rating < 3), 2) AS poor_query_percentage
92+
ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
8993
FROM Queries
90-
GROUP BY query_name;
94+
GROUP BY 1;
9195
```
9296

9397
<!-- tabs:end -->

solution/1200-1299/1211.Queries Quality and Percentage/Solution.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
SELECT
33
query_name,
44
ROUND(AVG(rating / position), 2) AS quality,
5-
ROUND(100 * AVG(rating < 3), 2) AS poor_query_percentage
5+
ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
66
FROM Queries
7-
GROUP BY query_name;
7+
GROUP BY 1;

solution/1600-1699/1633.Percentage of Users Attended a Contest/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%</pre>
9191

9292
<!-- 这里可写通用的实现逻辑 -->
9393

94+
**方法一:分组统计 + 子查询**
95+
96+
我们可以将 `Register` 表按照 `contest_id` 分组,统计每个赛事的注册人数,每个赛事的注册人数除以总注册人数即为该赛事的注册率。
97+
9498
<!-- tabs:start -->
9599

96100
### **SQL**
@@ -101,8 +105,8 @@ SELECT
101105
contest_id,
102106
ROUND(COUNT(1) * 100 / (SELECT COUNT(1) FROM Users), 2) AS percentage
103107
FROM Register
104-
GROUP BY contest_id
105-
ORDER BY percentage DESC, contest_id;
108+
GROUP BY 1
109+
ORDER BY 2 DESC, 1;
106110
```
107111

108112
<!-- tabs:end -->

solution/1600-1699/1633.Percentage of Users Attended a Contest/README_EN.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ Bob registered in contest 207 and the percentage is ((1/3) * 100) = 33.33%
8888

8989
## Solutions
9090

91+
**Solution 1: Grouping and Subquery**
92+
93+
We can group the `Register` table by `contest_id` and count the number of registrations for each contest. The registration rate of each contest is the number of registrations divided by the total number of registrations.
94+
9195
<!-- tabs:start -->
9296

9397
### **SQL**
@@ -98,8 +102,8 @@ SELECT
98102
contest_id,
99103
ROUND(COUNT(1) * 100 / (SELECT COUNT(1) FROM Users), 2) AS percentage
100104
FROM Register
101-
GROUP BY contest_id
102-
ORDER BY percentage DESC, contest_id;
105+
GROUP BY 1
106+
ORDER BY 2 DESC, 1;
103107
```
104108

105109
<!-- tabs:end -->

solution/1600-1699/1633.Percentage of Users Attended a Contest/Solution.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ SELECT
33
contest_id,
44
ROUND(COUNT(1) * 100 / (SELECT COUNT(1) FROM Users), 2) AS percentage
55
FROM Register
6-
GROUP BY contest_id
7-
ORDER BY percentage DESC, contest_id;
6+
GROUP BY 1
7+
ORDER BY 2 DESC, 1;

0 commit comments

Comments
 (0)