Skip to content

Commit 451ca21

Browse files
authored
feat: update solutions to lc problems: No.1173,1445 (#1787)
1 parent 4e141b3 commit 451ca21

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"solution/1300-1399/1322.Ads Performance/Solution.sql",
3232
"solution/1300-1399/1393.Capital GainLoss/Solution.sql",
3333
"solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/Solution.sql",
34+
"solution/1400-1499/1445.Apples & Oranges/Solution.sql",
3435
"solution/1400-1499/1479.Sales by Day of the Week/Solution.sql",
3536
"solution/1500-1599/1501.Countries You Can Safely Invest In/Solution.sql",
3637
"solution/1500-1599/1555.Bank Account Summary/Solution.sql",

solution/1100-1199/1173.Immediate Food Delivery I/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ Delivery 表:
5858

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

61+
**方法一:求和**
62+
63+
我们可以用 `sum` 函数来统计即时订单的数量,然后除以总订单数即可。由于题目求的是百分比,所以需要乘以 100,最后我们用 `round` 函数保留两位小数。
64+
6165
<!-- tabs:start -->
6266

6367
### **SQL**
6468

6569
```sql
6670
# Write your MySQL query statement below
6771
SELECT
68-
round(sum(order_date = customer_pref_delivery_date) * 100 / count(1), 2) AS immediate_percentage
72+
round(sum(order_date = customer_pref_delivery_date) / count(1) * 100, 2) AS immediate_percentage
6973
FROM Delivery;
7074
```
7175

solution/1100-1199/1173.Immediate Food Delivery I/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ Delivery table:
5454

5555
## Solutions
5656

57+
**Solution 1: Sum**
58+
59+
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.
60+
5761
<!-- tabs:start -->
5862

5963
### **SQL**
6064

6165
```sql
6266
# Write your MySQL query statement below
6367
SELECT
64-
round(sum(order_date = customer_pref_delivery_date) * 100 / count(1), 2) AS immediate_percentage
68+
round(sum(order_date = customer_pref_delivery_date) / count(1) * 100, 2) AS immediate_percentage
6569
FROM Delivery;
6670
```
6771

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Write your MySQL query statement below
22
SELECT
3-
round(sum(order_date = customer_pref_delivery_date) * 100 / count(1), 2) AS immediate_percentage
3+
round(sum(order_date = customer_pref_delivery_date) / count(1) * 100, 2) AS immediate_percentage
44
FROM Delivery;

solution/1400-1499/1445.Apples & Oranges/README.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ Sales</code> 表:
6767

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

70-
`CASE WHEN` + `GROUP BY`
70+
**方法一:分组求和**
71+
72+
我们可以将数据按照日期分组,然后用 `sum` 函数求出每天苹果和桔子的销售差异。如果是苹果,我们就用正数表示,如果是桔子,我们就用负数表示。最后我们按照日期排序即可。
7173

7274
<!-- tabs:start -->
7375

@@ -76,16 +78,11 @@ Sales</code> 表:
7678
```sql
7779
# Write your MySQL query statement below
7880
SELECT
79-
sale_date AS SALE_DATE,
80-
sum(
81-
CASE
82-
WHEN fruit = 'oranges' THEN -sold_num
83-
ELSE sold_num
84-
END
85-
) AS DIFF
81+
sale_date,
82+
sum(if(fruit = 'apples', sold_num, -sold_num)) AS diff
8683
FROM Sales
87-
GROUP BY sale_date
88-
ORDER BY sale_date;
84+
GROUP BY 1
85+
ORDER BY 1;
8986
```
9087

9188
<!-- tabs:end -->

solution/1400-1499/1445.Apples & Oranges/README_EN.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,22 @@ Day 2020-05-04, 15 apples and 16 oranges were sold (Difference 15 - 16 = -1).
6262

6363
## Solutions
6464

65+
**Solution 1: Group By + Sum**
66+
67+
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.
68+
6569
<!-- tabs:start -->
6670

6771
### **SQL**
6872

6973
```sql
7074
# Write your MySQL query statement below
7175
SELECT
72-
sale_date AS SALE_DATE,
73-
sum(
74-
CASE
75-
WHEN fruit = 'oranges' THEN -sold_num
76-
ELSE sold_num
77-
END
78-
) AS DIFF
76+
sale_date,
77+
sum(if(fruit = 'apples', sold_num, -sold_num)) AS diff
7978
FROM Sales
80-
GROUP BY sale_date
81-
ORDER BY sale_date;
79+
GROUP BY 1
80+
ORDER BY 1;
8281
```
8382

8483
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# Write your MySQL query statement below
22
SELECT
3-
sale_date AS SALE_DATE,
4-
sum(
5-
CASE
6-
WHEN fruit = 'oranges' THEN -sold_num
7-
ELSE sold_num
8-
END
9-
) AS DIFF
3+
sale_date,
4+
sum(if(fruit = 'apples', sold_num, -sold_num)) AS diff
105
FROM Sales
11-
GROUP BY sale_date
12-
ORDER BY sale_date;
6+
GROUP BY 1
7+
ORDER BY 1;

0 commit comments

Comments
 (0)