Skip to content

feat: update solutions to lc problems: No.1173,1445 #1787

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 11, 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
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"solution/1300-1399/1322.Ads Performance/Solution.sql",
"solution/1300-1399/1393.Capital GainLoss/Solution.sql",
"solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/Solution.sql",
"solution/1400-1499/1445.Apples & Oranges/Solution.sql",
"solution/1400-1499/1479.Sales by Day of the Week/Solution.sql",
"solution/1500-1599/1501.Countries You Can Safely Invest In/Solution.sql",
"solution/1500-1599/1555.Bank Account Summary/Solution.sql",
Expand Down
6 changes: 5 additions & 1 deletion solution/1100-1199/1173.Immediate Food Delivery I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ Delivery 表:

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

**方法一:求和**

我们可以用 `sum` 函数来统计即时订单的数量,然后除以总订单数即可。由于题目求的是百分比,所以需要乘以 100,最后我们用 `round` 函数保留两位小数。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
round(sum(order_date = customer_pref_delivery_date) * 100 / count(1), 2) AS immediate_percentage
round(sum(order_date = customer_pref_delivery_date) / count(1) * 100, 2) AS immediate_percentage
FROM Delivery;
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ Delivery table:

## Solutions

**Solution 1: Sum**

We can use the `sum` function to count the number of instant orders, and then divide it by the total number of orders. Since the problem requires a percentage, we need to multiply by 100. Finally, we can use the `round` function to keep two decimal places.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
round(sum(order_date = customer_pref_delivery_date) * 100 / count(1), 2) AS immediate_percentage
round(sum(order_date = customer_pref_delivery_date) / count(1) * 100, 2) AS immediate_percentage
FROM Delivery;
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Write your MySQL query statement below
SELECT
round(sum(order_date = customer_pref_delivery_date) * 100 / count(1), 2) AS immediate_percentage
round(sum(order_date = customer_pref_delivery_date) / count(1) * 100, 2) AS immediate_percentage
FROM Delivery;
17 changes: 7 additions & 10 deletions solution/1400-1499/1445.Apples & Oranges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ Sales</code> 表:

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

`CASE WHEN` + `GROUP BY`。
**方法一:分组求和**

我们可以将数据按照日期分组,然后用 `sum` 函数求出每天苹果和桔子的销售差异。如果是苹果,我们就用正数表示,如果是桔子,我们就用负数表示。最后我们按照日期排序即可。

<!-- tabs:start -->

Expand All @@ -76,16 +78,11 @@ Sales</code> 表:
```sql
# Write your MySQL query statement below
SELECT
sale_date AS SALE_DATE,
sum(
CASE
WHEN fruit = 'oranges' THEN -sold_num
ELSE sold_num
END
) AS DIFF
sale_date,
sum(if(fruit = 'apples', sold_num, -sold_num)) AS diff
FROM Sales
GROUP BY sale_date
ORDER BY sale_date;
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->
17 changes: 8 additions & 9 deletions solution/1400-1499/1445.Apples & Oranges/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,22 @@ Day 2020-05-04, 15 apples and 16 oranges were sold (Difference 15 - 16 = -1).

## Solutions

**Solution 1: Group By + Sum**

We can group the data by date, and then use the `sum` function to calculate the difference in sales between apples and oranges for each day. If it is an apple, we represent it with a positive number, and if it is an orange, we represent it with a negative number. Finally, we sort the data by date.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
sale_date AS SALE_DATE,
sum(
CASE
WHEN fruit = 'oranges' THEN -sold_num
ELSE sold_num
END
) AS DIFF
sale_date,
sum(if(fruit = 'apples', sold_num, -sold_num)) AS diff
FROM Sales
GROUP BY sale_date
ORDER BY sale_date;
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->
13 changes: 4 additions & 9 deletions solution/1400-1499/1445.Apples & Oranges/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# Write your MySQL query statement below
SELECT
sale_date AS SALE_DATE,
sum(
CASE
WHEN fruit = 'oranges' THEN -sold_num
ELSE sold_num
END
) AS DIFF
sale_date,
sum(if(fruit = 'apples', sold_num, -sold_num)) AS diff
FROM Sales
GROUP BY sale_date
ORDER BY sale_date;
GROUP BY 1
ORDER BY 1;