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
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"solution/1600-1699/1667.Fix Names in a Table/Solution.sql",
"solution/1700-1799/1777.Product's Price for Each Store/Solution.sql",
"solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql",
"solution/2000-2099/2066.Account Balance/Solution.sql",
"solution/2600-2699/2686.Immediate Food Delivery III/Solution.sql",
"solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql"
],
Expand Down
3 changes: 2 additions & 1 deletion solution/0100-0199/0192.Word Frequency/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ day 1
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sh

# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
```

<!-- tabs:end -->
3 changes: 2 additions & 1 deletion solution/0100-0199/0192.Word Frequency/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ day 1
### **Bash**

```sh

# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
```

<!-- tabs:end -->
19 changes: 2 additions & 17 deletions solution/0100-0199/0192.Word Frequency/Solution.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
#!/usr/bin/env bash

# NF是当前行的field字段数;NR是正在处理的当前行数。
awk '{
for(i=1;i<=NF;i++){
res[$i]++
}
}
END{
for(k in res){
print k,res[k]
}
}' words.txt | sort -nr -k2

#or:

cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2" "$1}'
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,33 @@ Experiments table:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
P AS (
SELECT 'Android' AS platform
UNION
SELECT 'IOS'
UNION
SELECT 'Web'
),
Exp AS (
SELECT 'Reading' AS experiment_name
UNION
SELECT 'Sports'
UNION
SELECT 'Programming'
),
T AS (
SELECT *
FROM
P,
Exp
)
SELECT platform, experiment_name, count(experiment_id) AS num_experiments
FROM
T AS t
LEFT JOIN Experiments USING (platform, experiment_name)
GROUP BY 1, 2;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,33 @@ On the platform &quot;Web&quot;, we had two &quot;Reading&quot; experiments and
### **SQL**

```sql

# Write your MySQL query statement below
WITH
P AS (
SELECT 'Android' AS platform
UNION
SELECT 'IOS'
UNION
SELECT 'Web'
),
Exp AS (
SELECT 'Reading' AS experiment_name
UNION
SELECT 'Sports'
UNION
SELECT 'Programming'
),
T AS (
SELECT *
FROM
P,
Exp
)
SELECT platform, experiment_name, count(experiment_id) AS num_experiments
FROM
T AS t
LEFT JOIN Experiments USING (platform, experiment_name)
GROUP BY 1, 2;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Write your MySQL query statement below
WITH
P AS (
SELECT 'Android' AS platform
UNION
SELECT 'IOS'
UNION
SELECT 'Web'
),
Exp AS (
SELECT 'Reading' AS experiment_name
UNION
SELECT 'Sports'
UNION
SELECT 'Programming'
),
T AS (
SELECT *
FROM
P,
Exp
)
SELECT platform, experiment_name, count(experiment_id) AS num_experiments
FROM
T AS t
LEFT JOIN Experiments USING (platform, experiment_name)
GROUP BY 1, 2;
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ Streams table:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT count(DISTINCT sub.account_id) AS accounts_count
FROM
Subscriptions AS sub
LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id
WHERE
year(start_date) <= 2021
AND year(end_date) >= 2021
AND (year(stream_date) != 2021 OR stream_date > end_date);
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ User 11 did not subscribe in 2021.
### **SQL**

```sql

# Write your MySQL query statement below
SELECT count(DISTINCT sub.account_id) AS accounts_count
FROM
Subscriptions AS sub
LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id
WHERE
year(start_date) <= 2021
AND year(end_date) >= 2021
AND (year(stream_date) != 2021 OR stream_date > end_date);
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Write your MySQL query statement below
SELECT count(DISTINCT sub.account_id) AS accounts_count
FROM
Subscriptions AS sub
LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id
WHERE
year(start_date) <= 2021
AND year(end_date) >= 2021
AND (year(stream_date) != 2021 OR stream_date > end_date);
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ Rounds table:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT candidate_id
FROM
Candidates AS c
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
WHERE years_of_exp >= 2
GROUP BY c.interview_id
HAVING sum(score) > 15;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ Rounds table:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT candidate_id
FROM
Candidates AS c
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
WHERE years_of_exp >= 2
GROUP BY c.interview_id
HAVING sum(score) > 15;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Write your MySQL query statement below
SELECT candidate_id
FROM
Candidates AS c
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
WHERE years_of_exp >= 2
GROUP BY c.interview_id
HAVING sum(score) > 15;
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@ Purchases 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT
m.member_id,
name,
CASE
WHEN count(v.visit_id) = 0 THEN 'Bronze'
WHEN 100 * count(charged_amount) / count(
v.visit_id
) >= 80 THEN 'Diamond'
WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold'
ELSE 'Silver'
END AS category
FROM
Members AS m
LEFT JOIN Visits AS v ON m.member_id = v.member_id
LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id
GROUP BY member_id;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,23 @@ Purchases table:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT
m.member_id,
name,
CASE
WHEN count(v.visit_id) = 0 THEN 'Bronze'
WHEN 100 * count(charged_amount) / count(
v.visit_id
) >= 80 THEN 'Diamond'
WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold'
ELSE 'Silver'
END AS category
FROM
Members AS m
LEFT JOIN Visits AS v ON m.member_id = v.member_id
LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id
GROUP BY member_id;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Write your MySQL query statement below
SELECT
m.member_id,
name,
CASE
WHEN count(v.visit_id) = 0 THEN 'Bronze'
WHEN 100 * count(charged_amount) / count(
v.visit_id
) >= 80 THEN 'Diamond'
WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold'
ELSE 'Silver'
END AS category
FROM
Members AS m
LEFT JOIN Visits AS v ON m.member_id = v.member_id
LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id
GROUP BY member_id;
11 changes: 10 additions & 1 deletion solution/2000-2099/2066.Account Balance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ Transactions 表:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
SELECT
account_id,
day,
sum(if(type = 'Deposit', amount, -amount)) OVER (
PARTITION BY account_id
ORDER BY day
) AS balance
FROM Transactions
ORDER BY 1, 2;
```

<!-- tabs:end -->
11 changes: 10 additions & 1 deletion solution/2000-2099/2066.Account Balance/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ Account 2:
### **SQL**

```sql

# Write your MySQL query statement below
SELECT
account_id,
day,
sum(if(type = 'Deposit', amount, -amount)) OVER (
PARTITION BY account_id
ORDER BY day
) AS balance
FROM Transactions
ORDER BY 1, 2;
```

<!-- tabs:end -->
10 changes: 10 additions & 0 deletions solution/2000-2099/2066.Account Balance/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Write your MySQL query statement below
SELECT
account_id,
day,
sum(if(type = 'Deposit', amount, -amount)) OVER (
PARTITION BY account_id
ORDER BY day
) AS balance
FROM Transactions
ORDER BY 1, 2;
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,18 @@ Orders table:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT customer_id
FROM Orders
WHERE order_type = 0
)
SELECT *
FROM Orders AS o
WHERE
order_type = 0
OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id);
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ Customer 4 has two orders of type 1. We return both of them.
### **SQL**

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT customer_id
FROM Orders
WHERE order_type = 0
)
SELECT *
FROM Orders AS o
WHERE
order_type = 0
OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id);
```

<!-- tabs:end -->
Loading