Skip to content

feat: add solutions to lc problems: No.1174,1193,1211,1633 #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions solution/1100-1199/1174.Immediate Food Delivery II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,47 @@ Delivery 表:

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

**方法一:子查询**

我们可以使用子查询,先找到每个用户的首次订单,然后再计算即时订单的比例。

**方法二:窗口函数**

我们可以使用 `RANK()` 窗口函数,按照每个用户的订单日期升序排列,获取到每个用户的订单排名,然后我们筛选出排名为 $1$ 的订单,即为首次订单,再计算即时订单的比例。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
select
ROUND(
SUM(
IF(
t1.order_date = t1.customer_pref_delivery_date,
1,
0
)
) / COUNT(1) * 100,
2
) as immediate_percentage
from
Delivery t1
right join (
select
customer_id,
MIN(order_date) as order_date
from
Delivery
group by
customer_id
) t2 on t1.customer_id = t2.customer_id
and t1.order_date = t2.order_date
SELECT
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
FROM Delivery
WHERE
(customer_id, order_date) IN (
SELECT customer_id, MIN(order_date)
FROM Delivery
GROUP BY 1
);
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS rk
FROM Delivery
)
SELECT
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
FROM T
WHERE rk = 1;
```

<!-- tabs:end -->
58 changes: 35 additions & 23 deletions solution/1100-1199/1174.Immediate Food Delivery II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,47 @@ Hence, half the customers have immediate first orders.

## Solutions

**Solution 1: Subquery**

We can use a subquery to first find the first order of each user, and then calculate the proportion of instant orders.

**Solution 2: Window Function**

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.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
select
ROUND(
SUM(
IF(
t1.order_date = t1.customer_pref_delivery_date,
1,
0
)
) / COUNT(1) * 100,
2
) as immediate_percentage
from
Delivery t1
right join (
select
customer_id,
MIN(order_date) as order_date
from
Delivery
group by
customer_id
) t2 on t1.customer_id = t2.customer_id
and t1.order_date = t2.order_date
SELECT
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
FROM Delivery
WHERE
(customer_id, order_date) IN (
SELECT customer_id, MIN(order_date)
FROM Delivery
GROUP BY 1
);
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
RANK() OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS rk
FROM Delivery
)
SELECT
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
FROM T
WHERE rk = 1;
```

<!-- tabs:end -->
20 changes: 7 additions & 13 deletions solution/1100-1199/1174.Immediate Food Delivery II/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# Write your MySQL query statement below
SELECT
ROUND(
SUM(IF(t1.order_date = t1.customer_pref_delivery_date, 1, 0)) / COUNT(1) * 100,
2
) AS immediate_percentage
FROM
Delivery AS t1
RIGHT JOIN (
SELECT
customer_id,
MIN(order_date) AS order_date
ROUND(SUM(order_date = customer_pref_delivery_date) / COUNT(1) * 100, 2) AS immediate_percentage
FROM Delivery
WHERE
(customer_id, order_date) IN (
SELECT customer_id, MIN(order_date)
FROM Delivery
GROUP BY customer_id
) AS t2
ON t1.customer_id = t2.customer_id AND t1.order_date = t2.order_date;
GROUP BY 1
);
4 changes: 4 additions & 0 deletions solution/1100-1199/1193.Monthly Transactions I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Transactions</code> table:

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

**方法一:分组求和**

我们可以先按照月份和国家分组,然后利用 `COUNT` 和 `SUM` 函数分别求出每个分组的事务数、已批准的事务数、总金额和已批准的总金额。

<!-- tabs:start -->

### **SQL**
Expand Down
4 changes: 4 additions & 0 deletions solution/1100-1199/1193.Monthly Transactions I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Transactions table:

## Solutions

**Solution 1: Grouping and Aggregation**

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.

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33

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

**方法一:分组统计**

我们将查询结果按照 `query_name` 进行分组,然后利用 `AVG` 和 `ROUND` 函数计算 `quality` 和 `poor_query_percentage`。

<!-- tabs:start -->

### **SQL**
Expand All @@ -90,9 +94,9 @@ Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33
SELECT
query_name,
ROUND(AVG(rating / position), 2) AS quality,
ROUND(100 * AVG(rating < 3), 2) AS poor_query_percentage
ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
FROM Queries
GROUP BY query_name;
GROUP BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33

## Solutions

**Solution 1: Grouping and Aggregation**

We can group the query results by `query_name`, and then use the `AVG` and `ROUND` functions to calculate `quality` and `poor_query_percentage`.

<!-- tabs:start -->

### **SQL**
Expand All @@ -85,9 +89,9 @@ Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
SELECT
query_name,
ROUND(AVG(rating / position), 2) AS quality,
ROUND(100 * AVG(rating < 3), 2) AS poor_query_percentage
ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
FROM Queries
GROUP BY query_name;
GROUP BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
SELECT
query_name,
ROUND(AVG(rating / position), 2) AS quality,
ROUND(100 * AVG(rating < 3), 2) AS poor_query_percentage
ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
FROM Queries
GROUP BY query_name;
GROUP BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%</pre>

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

**方法一:分组统计 + 子查询**

我们可以将 `Register` 表按照 `contest_id` 分组,统计每个赛事的注册人数,每个赛事的注册人数除以总注册人数即为该赛事的注册率。

<!-- tabs:start -->

### **SQL**
Expand All @@ -101,8 +105,8 @@ SELECT
contest_id,
ROUND(COUNT(1) * 100 / (SELECT COUNT(1) FROM Users), 2) AS percentage
FROM Register
GROUP BY contest_id
ORDER BY percentage DESC, contest_id;
GROUP BY 1
ORDER BY 2 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ Bob registered in contest 207 and the percentage is ((1/3) * 100) = 33.33%

## Solutions

**Solution 1: Grouping and Subquery**

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.

<!-- tabs:start -->

### **SQL**
Expand All @@ -98,8 +102,8 @@ SELECT
contest_id,
ROUND(COUNT(1) * 100 / (SELECT COUNT(1) FROM Users), 2) AS percentage
FROM Register
GROUP BY contest_id
ORDER BY percentage DESC, contest_id;
GROUP BY 1
ORDER BY 2 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ SELECT
contest_id,
ROUND(COUNT(1) * 100 / (SELECT COUNT(1) FROM Users), 2) AS percentage
FROM Register
GROUP BY contest_id
ORDER BY percentage DESC, contest_id;
GROUP BY 1
ORDER BY 2 DESC, 1;