Skip to content

feat: add sql solution to lc problem: No.2893 #1751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# [2893. Calculate Orders Within Each Interval](https://leetcode.cn/problems/calculate-orders-within-each-interval)

[English Version](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>Table: <code><font face="monospace">Orders</font></code></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| minute | int |
| order_count | int |
+-------------+------+
minute is the primary key for this table.
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.
</pre>

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

<ul>
<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>
</ul>

<p>Return<em> the result table ordered by <strong>interval_no</strong> in <strong>ascending</strong> order.</em></p>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
Orders table:
+--------+-------------+
| minute | order_count |
+--------+-------------+
| 1 | 0 |
| 2 | 2 |
| 3 | 4 |
| 4 | 6 |
| 5 | 1 |
| 6 | 4 |
| 7 | 1 |
| 8 | 2 |
| 9 | 4 |
| 10 | 1 |
| 11 | 4 |
| 12 | 6 |
+--------+-------------+
<strong>Output:</strong>
+-------------+--------------+
| interval_no | total_orders |
+-------------+--------------+
| 1 | 17 |
| 2 | 18 |
+-------------+--------------+
<strong>Explanation:</strong>
- Interval number 1 comprises minutes from 1 to 6. The total orders in these six minutes are (0 + 2 + 4 + 6 + 1 + 4) = 17.
- Interval number 2 comprises minutes from 7 to 12. The total orders in these six minutes are (1 + 2 + 4 + 1 + 4 + 6) = 18.
Returning table orderd by interval_no in ascending order.</pre>

## 解法

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

**方法一:窗口函数**

我们可以用窗口函数 `sum() over()` 来计算每 $6$ 分钟的订单总数,然后每条记录中的 `minute` 能被 $6$ 整除的记录。

<!-- tabs:start -->

### **SQL**

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

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
minute,
sum(order_count) OVER (
ORDER BY minute
ROWS 5 PRECEDING
) AS total_orders
FROM Orders
)
SELECT minute / 6 AS interval_no, total_orders
FROM T
WHERE minute % 6 = 0;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# [2893. Calculate Orders Within Each Interval](https://leetcode.com/problems/calculate-orders-within-each-interval)

[中文文档](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md)

## Description

<p>Table: <code><font face="monospace">Orders</font></code></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| minute | int |
| order_count | int |
+-------------+------+
minute is the primary key for this table.
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.
</pre>

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

<ul>
<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>
</ul>

<p>Return<em> the result table ordered by <strong>interval_no</strong> in <strong>ascending</strong> order.</em></p>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
Orders table:
+--------+-------------+
| minute | order_count |
+--------+-------------+
| 1 | 0 |
| 2 | 2 |
| 3 | 4 |
| 4 | 6 |
| 5 | 1 |
| 6 | 4 |
| 7 | 1 |
| 8 | 2 |
| 9 | 4 |
| 10 | 1 |
| 11 | 4 |
| 12 | 6 |
+--------+-------------+
<strong>Output:</strong>
+-------------+--------------+
| interval_no | total_orders |
+-------------+--------------+
| 1 | 17 |
| 2 | 18 |
+-------------+--------------+
<strong>Explanation:</strong>
- Interval number 1 comprises minutes from 1 to 6. The total orders in these six minutes are (0 + 2 + 4 + 6 + 1 + 4) = 17.
- Interval number 2 comprises minutes from 7 to 12. The total orders in these six minutes are (1 + 2 + 4 + 1 + 4 + 6) = 18.
Returning table orderd by interval_no in ascending order.</pre>

## Solutions

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
minute,
sum(order_count) OVER (
ORDER BY minute
ROWS 5 PRECEDING
) AS total_orders
FROM Orders
)
SELECT minute / 6 AS interval_no, total_orders
FROM T
WHERE minute % 6 = 0;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
minute,
sum(order_count) OVER (
ORDER BY minute
ROWS 5 PRECEDING
) AS total_orders
FROM Orders
)
SELECT minute / 6 AS interval_no, total_orders
FROM T
WHERE minute % 6 = 0;
7 changes: 4 additions & 3 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
| 1623 | [三人国家代表队](/solution/1600-1699/1623.All%20Valid%20Triplets%20That%20Can%20Represent%20a%20Country/README.md) | `数据库` | 简单 | 🔒 |
| 1633 | [各赛事的用户注册率](/solution/1600-1699/1633.Percentage%20of%20Users%20Attended%20a%20Contest/README.md) | `数据库` | 简单 | |
| 1635 | [Hopper 公司查询 I](/solution/1600-1699/1635.Hopper%20Company%20Queries%20I/README.md) | `数据库` | 困难 | 🔒 |
| 1645 | [1645.Hopper 公司查询 II](/solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README.md) | `数据库` | 困难 | 🔒 |
| 1645 | [Hopper 公司查询 II](/solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README.md) | `数据库` | 困难 | 🔒 |
| 1651 | [Hopper 公司查询 III](/solution/1600-1699/1651.Hopper%20Company%20Queries%20III/README.md) | `数据库` | 困难 | 🔒 |
| 1661 | [每台机器的进程平均运行时间](/solution/1600-1699/1661.Average%20Time%20of%20Process%20per%20Machine/README.md) | `数据库` | 简单 | |
| 1667 | [修复表中的名字](/solution/1600-1699/1667.Fix%20Names%20in%20a%20Table/README.md) | `数据库` | 简单 | |
Expand Down Expand Up @@ -243,8 +243,9 @@
| 2793 | [航班机票状态](/solution/2700-2799/2793.Status%20of%20Flight%20Tickets/README.md) | | 困难 | 🔒 |
| 2820 | [选举结果](/solution/2800-2899/2820.Election%20Results/README.md) | | 中等 | 🔒 |
| 2837 | [总旅行距离](/solution/2800-2899/2837.Total%20Traveled%20Distance/README.md) | `数据库` | 简单 | 🔒 |
| 2853 | [Highest Salaries Difference](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md) | | 简单 | 🔒 |
| 2854 | [Rolling Average Steps](/solution/2800-2899/2854.Rolling%20Average%20Steps/README.md) | | 中等 | 🔒 |
| 2853 | [最高薪水差异](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md) | `数据库` | 简单 | 🔒 |
| 2854 | [滚动平均步数](/solution/2800-2899/2854.Rolling%20Average%20Steps/README.md) | `数据库` | 中等 | 🔒 |
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) | | 中等 | 🔒 |

## 版权

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

## Copyright

Expand Down
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2902,6 +2902,7 @@
| 2889 | [Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README.md) | | 简单 | |
| 2890 | [Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README.md) | | 简单 | |
| 2891 | [Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README.md) | | 简单 | |
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) | | 中等 | 🔒 |

## 版权

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

## Copyright

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