|
| 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> </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 --> |
0 commit comments