Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3133ba5

Browse files
feat: add sql solutions to lc problems: No.1083,1084 (doocs#1198)
* No.1083.Sales Analysis II * No.1084.Sales Analysis III
1 parent 0ffa421 commit 3133ba5

File tree

6 files changed

+35
-78
lines changed

6 files changed

+35
-78
lines changed

‎solution/1000-1099/1083.Sales Analysis II/README.md‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ id 为 1 的买家购买了一部 S8,但是却没有购买 iPhone,而 id 为
7171

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

74+
**方法一:JOIN + GROUP BY + HAVING**
75+
76+
我们先将 `Sales` 表和 `Product` 表连接起来,然后根据 `buyer_id` 分组,最后用 `HAVING` 子句筛选出购买了 S8 却没有购买 iPhone 的买家。
77+
7478
<!-- tabs:start -->
7579

7680
### **SQL**
@@ -79,23 +83,10 @@ id 为 1 的买家购买了一部 S8,但是却没有购买 iPhone,而 id 为
7983
# Write your MySQL query statement below
8084
SELECT buyer_id
8185
FROM
82-
(
83-
SELECT
84-
buyer_id,
85-
CASE
86-
WHEN p.product_name = 'S8' THEN 1
87-
ELSE 0
88-
END AS s8,
89-
CASE
90-
WHEN p.product_name = 'iPhone' THEN 1
91-
ELSE 0
92-
END AS iPhone
93-
FROM
94-
Product AS p
95-
JOIN Sales AS s ON p.product_id = s.product_id
96-
) AS t
97-
GROUP BY buyer_id
98-
HAVING SUM(S8) > 0 AND SUM(iPhone) = 0;
86+
Sales
87+
JOIN Product USING (product_id)
88+
GROUP BY 1
89+
HAVING sum(product_name = 'S8') > 0 AND sum(product_name = 'iPhone') = 0;
9990
```
10091

10192
<!-- tabs:end -->

‎solution/1000-1099/1083.Sales Analysis II/README_EN.md‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,10 @@ Sales table:
8787
# Write your MySQL query statement below
8888
SELECT buyer_id
8989
FROM
90-
(
91-
SELECT
92-
buyer_id,
93-
CASE
94-
WHEN p.product_name = 'S8' THEN 1
95-
ELSE 0
96-
END AS s8,
97-
CASE
98-
WHEN p.product_name = 'iPhone' THEN 1
99-
ELSE 0
100-
END AS iPhone
101-
FROM
102-
Product AS p
103-
JOIN Sales AS s ON p.product_id = s.product_id
104-
) AS t
105-
GROUP BY buyer_id
106-
HAVING SUM(S8) > 0 AND SUM(iPhone) = 0;
90+
Sales
91+
JOIN Product USING (product_id)
92+
GROUP BY 1
93+
HAVING sum(product_name = 'S8') > 0 AND sum(product_name = 'iPhone') = 0;
10794
```
10895

10996
<!-- tabs:end -->
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
# Write your MySQL query statement below
22
SELECT buyer_id
33
FROM
4-
(
5-
SELECT
6-
buyer_id,
7-
CASE
8-
WHEN p.product_name = 'S8' THEN 1
9-
ELSE 0
10-
END AS s8,
11-
CASE
12-
WHEN p.product_name = 'iPhone' THEN 1
13-
ELSE 0
14-
END AS iPhone
15-
FROM
16-
Product AS p
17-
JOIN Sales AS s ON p.product_id = s.product_id
18-
) AS t
19-
GROUP BY buyer_id
20-
HAVING SUM(S8) > 0 AND SUM(iPhone) = 0;
4+
Sales
5+
JOIN Product USING (product_id)
6+
GROUP BY 1
7+
HAVING sum(product_name = 'S8') > 0 AND sum(product_name = 'iPhone') = 0;

‎solution/1000-1099/1084.Sales Analysis III/README.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,22 @@ id 3的产品在2019年春季之后销售。
8585

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

88+
**方法一:JOIN + GROUP BY + HAVING**
89+
90+
我们可以通过 `JOIN``Sales` 表和 `Product` 表连接起来,然后通过 `GROUP BY``HAVING` 来筛选出符合条件的产品。
91+
8892
<!-- tabs:start -->
8993

9094
### **SQL**
9195

9296
```sql
9397
# Write your MySQL query statement below
94-
SELECT
95-
p.product_id,
96-
p.product_name
98+
SELECT product_id, product_name
9799
FROM
98-
Product AS p
99-
JOIN Sales AS s ON p.product_id = s.product_id
100-
GROUP BY p.product_id
101-
HAVING
102-
SUM(s.sale_date < '2019年01月01日') = 0
103-
AND SUM(s.sale_date > '2019年03月31日') = 0;
100+
Sales
101+
JOIN Product USING (product_id)
102+
GROUP BY 1
103+
HAVING count(1) = sum(sale_date BETWEEN '2019年01月01日' AND '2019年03月31日');
104104
```
105105

106106
<!-- tabs:end -->

‎solution/1000-1099/1084.Sales Analysis III/README_EN.md‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,12 @@ We return only product 1 as it is the product that was only sold in the spring o
8787

8888
```sql
8989
# Write your MySQL query statement below
90-
SELECT
91-
p.product_id,
92-
p.product_name
90+
SELECT product_id, product_name
9391
FROM
94-
Product AS p
95-
JOIN Sales AS s ON p.product_id = s.product_id
96-
GROUP BY p.product_id
97-
HAVING
98-
SUM(s.sale_date < '2019年01月01日') = 0
99-
AND SUM(s.sale_date > '2019年03月31日') = 0;
92+
Sales
93+
JOIN Product USING (product_id)
94+
GROUP BY 1
95+
HAVING count(1) = sum(sale_date BETWEEN '2019年01月01日' AND '2019年03月31日');
10096
```
10197

10298
<!-- tabs:end -->
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
p.product_id,
4-
p.product_name
2+
SELECT product_id, product_name
53
FROM
6-
Product AS p
7-
JOIN Sales AS s ON p.product_id = s.product_id
8-
GROUP BY p.product_id
9-
HAVING
10-
SUM(s.sale_date < '2019年01月01日') = 0
11-
AND SUM(s.sale_date > '2019年03月31日') = 0;
4+
Sales
5+
JOIN Product USING (product_id)
6+
GROUP BY 1
7+
HAVING count(1) = sum(sale_date BETWEEN '2019年01月01日' AND '2019年03月31日');

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /