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 04924f4

Browse files
committed
feat: add sql solution to lc problems: No.1158,1393,1407
- No.1158.Market Analysis I - No.1393.Capital GainLoss - No.1407.Top Travellers
1 parent be3a2aa commit 04924f4

File tree

9 files changed

+102
-29
lines changed

9 files changed

+102
-29
lines changed

‎solution/1100-1199/1158.Market Analysis I/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,29 @@ Items 表:
114114
### **SQL**
115115

116116
```sql
117+
SELECT user_id AS buyer_id,
118+
join_date,
119+
COUNT(order_id) AS orders_in_2019
120+
FROM users AS u
121+
LEFT JOIN orders AS o ON u.user_id = o.buyer_id
122+
AND YEAR(order_date) = 2019
123+
GROUP BY user_id;
124+
```
117125

126+
```sql
127+
SELECT
128+
user_id AS buyer_id,
129+
join_date,
130+
(
131+
SELECT
132+
COUNT(*)
133+
FROM
134+
orders AS o
135+
WHERE
136+
u.user_id = o.buyer_id AND YEAR(order_date) = 2019
137+
) AS orders_in_2019
138+
FROM
139+
users AS u;
118140
```
119141

120142
<!-- tabs:end -->

‎solution/1100-1199/1158.Market Analysis I/README_EN.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,29 @@ Items table:
111111
### **SQL**
112112

113113
```sql
114+
SELECT user_id AS buyer_id,
115+
join_date,
116+
COUNT(order_id) AS orders_in_2019
117+
FROM users AS u
118+
LEFT JOIN orders AS o ON u.user_id = o.buyer_id
119+
AND YEAR(order_date) = 2019
120+
GROUP BY user_id;
121+
```
114122

123+
```sql
124+
SELECT
125+
user_id AS buyer_id,
126+
join_date,
127+
(
128+
SELECT
129+
COUNT(*)
130+
FROM
131+
orders AS o
132+
WHERE
133+
u.user_id = o.buyer_id AND YEAR(order_date) = 2019
134+
) AS orders_in_2019
135+
FROM
136+
users AS u;
115137
```
116138

117139
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT user_id AS buyer_id,
2+
join_date,
3+
COUNT(order_id) AS orders_in_2019
4+
FROM users AS u
5+
LEFT JOIN orders AS o ON u.user_id = o.buyer_id
6+
AND YEAR(order_date) = 2019
7+
GROUP BY user_id;

‎solution/1300-1399/1393.Capital GainLoss/README.md‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,15 @@ Corona Masks 股票在第1天以10美元的价格买入,在第3天以1010美
7272
### **SQL**
7373

7474
```sql
75-
# Write your MySQL query statement below
76-
SELECT
77-
stock_name,
78-
sum(
79-
CASE WHEN operation = 'Buy' THEN -price ELSE price END
75+
SELECT stock_name,
76+
SUM(
77+
CASE
78+
WHEN operation = 'Buy' THEN - price
79+
ELSE price
80+
END
8081
) AS capital_gain_loss
81-
FROM
82-
Stocks
83-
GROUP BY
84-
stock_name;
82+
FROM Stocks
83+
GROUP BY stock_name;
8584
```
8685

8786
<!-- tabs:end -->

‎solution/1300-1399/1393.Capital GainLoss/README_EN.md‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,15 @@ Corona Masks stock was bought at day 1 for 10$ and was sold at day 3 for 1010$.
7272
### **SQL**
7373

7474
```sql
75-
# Write your MySQL query statement below
76-
SELECT
77-
stock_name,
78-
sum(
79-
CASE WHEN operation = 'Buy' THEN -price ELSE price END
75+
SELECT stock_name,
76+
SUM(
77+
CASE
78+
WHEN operation = 'Buy' THEN - price
79+
ELSE price
80+
END
8081
) AS capital_gain_loss
81-
FROM
82-
Stocks
83-
GROUP BY
84-
stock_name;
82+
FROM Stocks
83+
GROUP BY stock_name;
8584
```
8685

8786
<!-- tabs:end -->
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Write your MySQL query statement below
2-
SELECT
3-
stock_name,
4-
sum(
5-
CASE WHEN operation = 'Buy' THEN -price ELSE price END
1+
SELECT stock_name,
2+
SUM(
3+
CASE
4+
WHEN operation = 'Buy' THEN - price
5+
ELSE price
6+
END
67
) AS capital_gain_loss
7-
FROM
8-
Stocks
9-
GROUP BY
10-
stock_name;
8+
FROM Stocks
9+
GROUP BY stock_name;

‎solution/1400-1499/1407.Top Travellers/README.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ Donald 没有任何行程, 他的旅行距离为 0。
9797
### **SQL**
9898

9999
```sql
100-
100+
SELECT name,
101+
COALESCE(SUM(distance), 0) AS travelled_distance
102+
FROM Users AS u
103+
LEFT JOIN Rides AS r ON u.id = r.user_id
104+
GROUP BY
105+
name
106+
ORDER BY
107+
travelled_distance DESC,
108+
name;
101109
```
102110

103111
<!-- tabs:end -->

‎solution/1400-1499/1407.Top Travellers/README_EN.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ Donald did not have any rides, the distance traveled by him is 0.
9797
### **SQL**
9898

9999
```sql
100-
100+
SELECT name,
101+
COALESCE(SUM(distance), 0) AS travelled_distance
102+
FROM Users AS u
103+
LEFT JOIN Rides AS r ON u.id = r.user_id
104+
GROUP BY
105+
name
106+
ORDER BY
107+
travelled_distance DESC,
108+
name;
101109
```
102110

103111
<!-- tabs:end -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SELECT name,
2+
COALESCE(SUM(distance), 0) AS travelled_distance
3+
FROM Users AS u
4+
LEFT JOIN Rides AS r ON u.id = r.user_id
5+
GROUP BY
6+
name
7+
ORDER BY
8+
travelled_distance DESC,
9+
name;

0 commit comments

Comments
(0)

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