Skip to content
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ node_modules/
/solution/1600-1699/1613.Find the Missing IDs/Solution.sql
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,27 @@ startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000

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

**方法一:使用 count(distinct) 函数**

注意需要判断的是单次购买金额是否大于等于 `minAmount`,而不是累计购买金额是否大于等于 `minAmount`。

<!-- tabs:start -->

### **SQL**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
# Write your MySQL query statement below.
SELECT count(DISTINCT user_id) AS user_cnt
FROM Purchases
WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount;
);
END
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ Out of the three users, only User 3 is eligible for a discount.
### **SQL**

```sql

CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
# Write your MySQL query statement below.
SELECT count(DISTINCT user_id) AS user_cnt
FROM Purchases
WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount;
);
END
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
# Write your MySQL query statement below.
SELECT count(DISTINCT user_id) AS user_cnt
FROM Purchases
WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount;
);
END
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,33 @@ Orders 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql
# Write your MySQL query statement below
WITH
P AS (
SELECT product_id, year(purchase_date) AS y, count(1) >= 3 AS mark
FROM Orders
GROUP BY 1, 2
)
SELECT DISTINCT p1.product_id
FROM
P AS p1
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id
WHERE p1.mark AND p2.mark;
```

```sql
# Write your MySQL query statement below
WITH
P AS (
SELECT product_id, year(purchase_date) AS y
FROM Orders
GROUP BY 1, 2
HAVING count(1) >= 3
)
SELECT DISTINCT p1.product_id
FROM
P AS p1
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,33 @@ Product 2 was ordered one time in 2022. We do not include it in the answer.
### **SQL**

```sql
# Write your MySQL query statement below
WITH
P AS (
SELECT product_id, year(purchase_date) AS y, count(1) >= 3 AS mark
FROM Orders
GROUP BY 1, 2
)
SELECT DISTINCT p1.product_id
FROM
P AS p1
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id
WHERE p1.mark AND p2.mark;
```

```sql
# Write your MySQL query statement below
WITH
P AS (
SELECT product_id, year(purchase_date) AS y
FROM Orders
GROUP BY 1, 2
HAVING count(1) >= 3
)
SELECT DISTINCT p1.product_id
FROM
P AS p1
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Write your MySQL query statement below
WITH
P AS (
SELECT product_id, year(purchase_date) AS y
FROM Orders
GROUP BY 1, 2
HAVING count(1) >= 3
)
SELECT DISTINCT p1.product_id
FROM
P AS p1
JOIN P AS p2 ON p1.y = p2.y - 1 AND p1.product_id = p2.product_id;
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,21 @@ Weather 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
rank() OVER (
PARTITION BY city_id
ORDER BY degree DESC, day
) AS rk
FROM Weather
)
SELECT city_id, day, degree
FROM T
WHERE rk = 1
ORDER BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,21 @@ For city 3, the maximum degree was recorded on 2022-12-07 with -6 degrees.
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
rank() OVER (
PARTITION BY city_id
ORDER BY degree DESC, day
) AS rk
FROM Weather
)
SELECT city_id, day, degree
FROM T
WHERE rk = 1
ORDER BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
rank() OVER (
PARTITION BY city_id
ORDER BY degree DESC, day
) AS rk
FROM Weather
)
SELECT city_id, day, degree
FROM T
WHERE rk = 1
ORDER BY 1;
19 changes: 18 additions & 1 deletion solution/2300-2399/2324.Product Sales Analysis IV/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,24 @@ Product 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
user_id,
product_id,
rank() OVER (
PARTITION BY user_id
ORDER BY sum(quantity * price) DESC
) AS rk
FROM
Sales
JOIN Product USING (product_id)
GROUP BY 1, 2
)
SELECT user_id, product_id
FROM T
WHERE rk = 1;
```

<!-- tabs:end -->
19 changes: 18 additions & 1 deletion solution/2300-2399/2324.Product Sales Analysis IV/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,24 @@ User 102 spent the most money on products 1, 2, and 3.
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT
user_id,
product_id,
rank() OVER (
PARTITION BY user_id
ORDER BY sum(quantity * price) DESC
) AS rk
FROM
Sales
JOIN Product USING (product_id)
GROUP BY 1, 2
)
SELECT user_id, product_id
FROM T
WHERE rk = 1;
```

<!-- tabs:end -->
18 changes: 18 additions & 0 deletions solution/2300-2399/2324.Product Sales Analysis IV/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
user_id,
product_id,
rank() OVER (
PARTITION BY user_id
ORDER BY sum(quantity * price) DESC
) AS rk
FROM
Sales
JOIN Product USING (product_id)
GROUP BY 1, 2
)
SELECT user_id, product_id
FROM T
WHERE rk = 1;
8 changes: 7 additions & 1 deletion solution/2300-2399/2329.Product Sales Analysis V/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ Product 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT user_id, sum(quantity * price) AS spending
FROM
Sales
JOIN Product USING (product_id)
GROUP BY 1
ORDER BY 2 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ Users 102 and 103 spent the same amount and we break the tie by their ID while u
### **SQL**

```sql

# Write your MySQL query statement below
SELECT user_id, sum(quantity * price) AS spending
FROM
Sales
JOIN Product USING (product_id)
GROUP BY 1
ORDER BY 2 DESC, 1;
```

<!-- tabs:end -->
7 changes: 7 additions & 0 deletions solution/2300-2399/2329.Product Sales Analysis V/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Write your MySQL query statement below
SELECT user_id, sum(quantity * price) AS spending
FROM
Sales
JOIN Product USING (product_id)
GROUP BY 1
ORDER BY 2 DESC, 1;
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ Teams 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT t1.team_name AS home_team, t2.team_name AS away_team
FROM
Teams AS t1
CROSS JOIN Teams AS t2
WHERE t1.team_name != t2.team_name;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ Teams table:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT t1.team_name AS home_team, t2.team_name AS away_team
FROM
Teams AS t1
CROSS JOIN Teams AS t2
WHERE t1.team_name != t2.team_name;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Write your MySQL query statement below
SELECT t1.team_name AS home_team, t2.team_name AS away_team
FROM
Teams AS t1
CROSS JOIN Teams AS t2
WHERE t1.team_name != t2.team_name;
5 changes: 4 additions & 1 deletion solution/2300-2399/2377.Sort the Olympic Table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ Olympic 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT *
FROM Olympic
ORDER BY 2 DESC, 3 DESC, 4 DESC, 1;
```

<!-- tabs:end -->
5 changes: 4 additions & 1 deletion solution/2300-2399/2377.Sort the Olympic Table/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ Israel comes before Egypt because it has more bronze medals.
### **SQL**

```sql

# Write your MySQL query statement below
SELECT *
FROM Olympic
ORDER BY 2 DESC, 3 DESC, 4 DESC, 1;
```

<!-- tabs:end -->
4 changes: 4 additions & 0 deletions solution/2300-2399/2377.Sort the Olympic Table/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Write your MySQL query statement below
SELECT *
FROM Olympic
ORDER BY 2 DESC, 3 DESC, 4 DESC, 1;