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 fe575a0

Browse files
feat: add sql solution to lc problem: No.2893 (doocs#1751)
No.2893.Calculate Orders Within Each Interval
1 parent 17cedee commit fe575a0

File tree

11 files changed

+214
-8
lines changed

11 files changed

+214
-8
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2893. Calculate Orders Within Each Interval](https://leetcode.cn/problems/calculate-orders-within-each-interval)
2+
3+
[English Version](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code><font face="monospace">Orders</font></code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| minute | int |
16+
| order_count | int |
17+
+-------------+------+
18+
minute is the primary key for this table.
19+
Each row of this table contains the minute and number of orders received during that specific minute. The total number of rows will be a multiple of 6.
20+
</pre>
21+
22+
<p>Write a query to calculate <strong>total</strong> <strong>orders</strong><b> </b>within each <strong>interval</strong>. Each interval is defined as a combination of <code>6</code> minutes.</p>
23+
24+
<ul>
25+
<li>Minutes <code>1</code> to <code>6</code> fall within interval <code>1</code>, while minutes <code>7</code> to <code>12</code> belong to interval <code>2</code>, and so forth.</li>
26+
</ul>
27+
28+
<p>Return<em> the result table ordered by <strong>interval_no</strong> in <strong>ascending</strong> order.</em></p>
29+
30+
<p>The result format is in the following example.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong class="example">Example 1:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong>
37+
Orders table:
38+
+--------+-------------+
39+
| minute | order_count |
40+
+--------+-------------+
41+
| 1 | 0 |
42+
| 2 | 2 |
43+
| 3 | 4 |
44+
| 4 | 6 |
45+
| 5 | 1 |
46+
| 6 | 4 |
47+
| 7 | 1 |
48+
| 8 | 2 |
49+
| 9 | 4 |
50+
| 10 | 1 |
51+
| 11 | 4 |
52+
| 12 | 6 |
53+
+--------+-------------+
54+
<strong>Output:</strong>
55+
+-------------+--------------+
56+
| interval_no | total_orders |
57+
+-------------+--------------+
58+
| 1 | 17 |
59+
| 2 | 18 |
60+
+-------------+--------------+
61+
<strong>Explanation:</strong>
62+
- Interval number 1 comprises minutes from 1 to 6. The total orders in these six minutes are (0 + 2 + 4 + 6 + 1 + 4) = 17.
63+
- Interval number 2 comprises minutes from 7 to 12. The total orders in these six minutes are (1 + 2 + 4 + 1 + 4 + 6) = 18.
64+
Returning table orderd by interval_no in ascending order.</pre>
65+
66+
## 解法
67+
68+
<!-- 这里可写通用的实现逻辑 -->
69+
70+
**方法一:窗口函数**
71+
72+
我们可以用窗口函数 `sum() over()` 来计算每 6ドル$ 分钟的订单总数,然后每条记录中的 `minute` 能被 6ドル$ 整除的记录。
73+
74+
<!-- tabs:start -->
75+
76+
### **SQL**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```sql
81+
# Write your MySQL query statement below
82+
WITH
83+
T AS (
84+
SELECT
85+
minute,
86+
sum(order_count) OVER (
87+
ORDER BY minute
88+
ROWS 5 PRECEDING
89+
) AS total_orders
90+
FROM Orders
91+
)
92+
SELECT minute / 6 AS interval_no, total_orders
93+
FROM T
94+
WHERE minute % 6 = 0;
95+
```
96+
97+
<!-- tabs:end -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2893. Calculate Orders Within Each Interval](https://leetcode.com/problems/calculate-orders-within-each-interval)
2+
3+
[中文文档](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code><font face="monospace">Orders</font></code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| minute | int |
14+
| order_count | int |
15+
+-------------+------+
16+
minute is the primary key for this table.
17+
Each row of this table contains the minute and number of orders received during that specific minute. The total number of rows will be a multiple of 6.
18+
</pre>
19+
20+
<p>Write a query to calculate <strong>total</strong> <strong>orders</strong><b> </b>within each <strong>interval</strong>. Each interval is defined as a combination of <code>6</code> minutes.</p>
21+
22+
<ul>
23+
<li>Minutes <code>1</code> to <code>6</code> fall within interval <code>1</code>, while minutes <code>7</code> to <code>12</code> belong to interval <code>2</code>, and so forth.</li>
24+
</ul>
25+
26+
<p>Return<em> the result table ordered by <strong>interval_no</strong> in <strong>ascending</strong> order.</em></p>
27+
28+
<p>The result format is in the following example.</p>
29+
30+
<p>&nbsp;</p>
31+
<p><strong class="example">Example 1:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong>
35+
Orders table:
36+
+--------+-------------+
37+
| minute | order_count |
38+
+--------+-------------+
39+
| 1 | 0 |
40+
| 2 | 2 |
41+
| 3 | 4 |
42+
| 4 | 6 |
43+
| 5 | 1 |
44+
| 6 | 4 |
45+
| 7 | 1 |
46+
| 8 | 2 |
47+
| 9 | 4 |
48+
| 10 | 1 |
49+
| 11 | 4 |
50+
| 12 | 6 |
51+
+--------+-------------+
52+
<strong>Output:</strong>
53+
+-------------+--------------+
54+
| interval_no | total_orders |
55+
+-------------+--------------+
56+
| 1 | 17 |
57+
| 2 | 18 |
58+
+-------------+--------------+
59+
<strong>Explanation:</strong>
60+
- Interval number 1 comprises minutes from 1 to 6. The total orders in these six minutes are (0 + 2 + 4 + 6 + 1 + 4) = 17.
61+
- Interval number 2 comprises minutes from 7 to 12. The total orders in these six minutes are (1 + 2 + 4 + 1 + 4 + 6) = 18.
62+
Returning table orderd by interval_no in ascending order.</pre>
63+
64+
## Solutions
65+
66+
<!-- tabs:start -->
67+
68+
### **SQL**
69+
70+
```sql
71+
# Write your MySQL query statement below
72+
WITH
73+
T AS (
74+
SELECT
75+
minute,
76+
sum(order_count) OVER (
77+
ORDER BY minute
78+
ROWS 5 PRECEDING
79+
) AS total_orders
80+
FROM Orders
81+
)
82+
SELECT minute / 6 AS interval_no, total_orders
83+
FROM T
84+
WHERE minute % 6 = 0;
85+
```
86+
87+
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
minute,
6+
sum(order_count) OVER (
7+
ORDER BY minute
8+
ROWS 5 PRECEDING
9+
) AS total_orders
10+
FROM Orders
11+
)
12+
SELECT minute / 6 AS interval_no, total_orders
13+
FROM T
14+
WHERE minute % 6 = 0;

‎solution/DATABASE_README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
| 1623 | [三人国家代表队](/solution/1600-1699/1623.All%20Valid%20Triplets%20That%20Can%20Represent%20a%20Country/README.md) | `数据库` | 简单 | 🔒 |
145145
| 1633 | [各赛事的用户注册率](/solution/1600-1699/1633.Percentage%20of%20Users%20Attended%20a%20Contest/README.md) | `数据库` | 简单 | |
146146
| 1635 | [Hopper 公司查询 I](/solution/1600-1699/1635.Hopper%20Company%20Queries%20I/README.md) | `数据库` | 困难 | 🔒 |
147-
| 1645 | [1645.Hopper 公司查询 II](/solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README.md) | `数据库` | 困难 | 🔒 |
147+
| 1645 | [Hopper 公司查询 II](/solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README.md) | `数据库` | 困难 | 🔒 |
148148
| 1651 | [Hopper 公司查询 III](/solution/1600-1699/1651.Hopper%20Company%20Queries%20III/README.md) | `数据库` | 困难 | 🔒 |
149149
| 1661 | [每台机器的进程平均运行时间](/solution/1600-1699/1661.Average%20Time%20of%20Process%20per%20Machine/README.md) | `数据库` | 简单 | |
150150
| 1667 | [修复表中的名字](/solution/1600-1699/1667.Fix%20Names%20in%20a%20Table/README.md) | `数据库` | 简单 | |
@@ -243,8 +243,9 @@
243243
| 2793 | [航班机票状态](/solution/2700-2799/2793.Status%20of%20Flight%20Tickets/README.md) | | 困难 | 🔒 |
244244
| 2820 | [选举结果](/solution/2800-2899/2820.Election%20Results/README.md) | | 中等 | 🔒 |
245245
| 2837 | [总旅行距离](/solution/2800-2899/2837.Total%20Traveled%20Distance/README.md) | `数据库` | 简单 | 🔒 |
246-
| 2853 | [Highest Salaries Difference](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md) | | 简单 | 🔒 |
247-
| 2854 | [Rolling Average Steps](/solution/2800-2899/2854.Rolling%20Average%20Steps/README.md) | | 中等 | 🔒 |
246+
| 2853 | [最高薪水差异](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md) | `数据库` | 简单 | 🔒 |
247+
| 2854 | [滚动平均步数](/solution/2800-2899/2854.Rolling%20Average%20Steps/README.md) | `数据库` | 中等 | 🔒 |
248+
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) | | 中等 | 🔒 |
248249

249250
## 版权
250251

‎solution/DATABASE_README_EN.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
241241
| 2793 | [Status of Flight Tickets](/solution/2700-2799/2793.Status%20of%20Flight%20Tickets/README_EN.md) | | Hard | 🔒 |
242242
| 2820 | [Election Results](/solution/2800-2899/2820.Election%20Results/README_EN.md) | | Medium | 🔒 |
243243
| 2837 | [Total Traveled Distance](/solution/2800-2899/2837.Total%20Traveled%20Distance/README_EN.md) | `Database` | Easy | 🔒 |
244-
| 2853 | [Highest Salaries Difference](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md) | | Easy | 🔒 |
245-
| 2854 | [Rolling Average Steps](/solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md) | | Medium | 🔒 |
244+
| 2853 | [Highest Salaries Difference](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md) | `Database` | Easy | 🔒 |
245+
| 2854 | [Rolling Average Steps](/solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md) | `Database` | Medium | 🔒 |
246+
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md) | | Medium | 🔒 |
246247

247248
## Copyright
248249

‎solution/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,7 @@
29022902
| 2889 | [Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README.md) | | 简单 | |
29032903
| 2890 | [Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README.md) | | 简单 | |
29042904
| 2891 | [Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README.md) | | 简单 | |
2905+
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) | | 中等 | 🔒 |
29052906

29062907
## 版权
29072908

‎solution/README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
29002900
| 2889 | [Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README_EN.md) | | Easy | |
29012901
| 2890 | [Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README_EN.md) | | Easy | |
29022902
| 2891 | [Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README_EN.md) | | Easy | |
2903+
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md) | | Medium | 🔒 |
29032904

29042905
## Copyright
29052906

‎solution/database-summary.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
- [1623.三人国家代表队](/database-solution/1600-1699/1623.All%20Valid%20Triplets%20That%20Can%20Represent%20a%20Country/README.md)
135135
- [1633.各赛事的用户注册率](/database-solution/1600-1699/1633.Percentage%20of%20Users%20Attended%20a%20Contest/README.md)
136136
- [1635.Hopper 公司查询 I](/database-solution/1600-1699/1635.Hopper%20Company%20Queries%20I/README.md)
137-
- [1645.1645.Hopper 公司查询 II](/database-solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README.md)
137+
- [1645.Hopper 公司查询 II](/database-solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README.md)
138138
- [1651.Hopper 公司查询 III](/database-solution/1600-1699/1651.Hopper%20Company%20Queries%20III/README.md)
139139
- [1661.每台机器的进程平均运行时间](/database-solution/1600-1699/1661.Average%20Time%20of%20Process%20per%20Machine/README.md)
140140
- [1667.修复表中的名字](/database-solution/1600-1699/1667.Fix%20Names%20in%20a%20Table/README.md)
@@ -233,5 +233,6 @@
233233
- [2793.航班机票状态](/database-solution/2700-2799/2793.Status%20of%20Flight%20Tickets/README.md)
234234
- [2820.选举结果](/database-solution/2800-2899/2820.Election%20Results/README.md)
235235
- [2837.总旅行距离](/database-solution/2800-2899/2837.Total%20Traveled%20Distance/README.md)
236-
- [2853.Highest Salaries Difference](/database-solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md)
237-
- [2854.Rolling Average Steps](/database-solution/2800-2899/2854.Rolling%20Average%20Steps/README.md)
236+
- [2853.最高薪水差异](/database-solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md)
237+
- [2854.滚动平均步数](/database-solution/2800-2899/2854.Rolling%20Average%20Steps/README.md)
238+
- [2893.Calculate Orders Within Each Interval](/database-solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md)

‎solution/database-summary_en.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,4 @@
235235
- [2837.Total Traveled Distance](/database-solution/2800-2899/2837.Total%20Traveled%20Distance/README_EN.md)
236236
- [2853.Highest Salaries Difference](/database-solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md)
237237
- [2854.Rolling Average Steps](/database-solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md)
238+
- [2893.Calculate Orders Within Each Interval](/database-solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md)

‎solution/summary.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,3 +2947,4 @@
29472947
- [2889.Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README.md)
29482948
- [2890.Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README.md)
29492949
- [2891.Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README.md)
2950+
- [2893.Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md)

0 commit comments

Comments
(0)

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