返回介绍

solution / 2800-2899 / 2893.Calculate Orders Within Each Interval / README_EN

发布于 2024-06-17 01:02:59 字数 3017 浏览 0 评论 0 收藏 0

2893. Calculate Orders Within Each Interval

中文文档

Description

Table: Orders

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

Write a query to calculate total orders within each interval. Each interval is defined as a combination of 6 minutes.

  • Minutes 1 to 6 fall within interval 1, while minutes 7 to 12 belong to interval 2, and so forth.

Return_ the result table ordered by interval_no in ascending order._

The result format is in the following example.

 

Example 1:

Input: 
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       | 
+--------+-------------+
Output: 
+-------------+--------------+
| interval_no | total_orders | 
+-------------+--------------+
| 1       | 17       | 
| 2       | 18       |  
+-------------+--------------+
Explanation: 
- 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.

Solutions

Solution 1

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

Solution 2

SELECT
  FLOOR((minute + 5) / 6) AS interval_no,
  SUM(order_count) AS total_orders
FROM Orders
GROUP BY 1
ORDER BY 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文