sql - select case语句添加时间截止

sql - select case statement to add time cut-off

我有这个 table,其中包含创建订单时间,以及接收、处理、分配和交付时间。以及计算他们的平均时间的视图。我正在尝试添加一个条款,在 8:00PM(关闭时间)之后提交的订单被视为第二天 8:00AM(营业时间)以保持正确的平均时间。这是命令 [![table: ][1]][1] 这是我用来计算平均时间的视图

SELECT 
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`received_time`,
            `sales`.`inprocess_time`)) AS `avg_prepare`,
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`inprocess_time`,
            `sales`.`assigned_time`)) AS `avg_assign`,
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`assigned_time`,
            `sales`.`delivered_time`)) AS `avg_delivery`,
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`received_time`,
            `sales`.`delivered_time`)) AS `avg_total`
    FROM
        `customer_orders` `sales`

感谢您的帮助!

SELECT 
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`received_time`,
            `sales`.`inprocess_time`)) AS `avg_prepare`,
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`inprocess_time`,
            `sales`.`assigned_time`)) AS `avg_assign`,
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`assigned_time`,
            `sales`.`delivered_time`)) AS `avg_delivery`,
        AVG(TIMESTAMPDIFF(MINUTE,
            `sales`.`received_time`,
            `sales`.`delivered_time`)) AS `avg_total`
    FROM
        (SELECT 
              CASE WHEN TIME(received_time) > '20:00:00' THEN DATE_ADD(DATE(received_time), INTERVAL 32 HOUR) 
                   WHEN TIME(received_time) <= '07:59:59' THEN DATE_ADD(DATE(received_time), INTERVAL 8 HOUR) 
                   ELSE received_time 
              END as received_time
           ,inprocess_time
           ,assigned_time
           ,delivered_time
        FROM `customer_orders`
        WHERE created_on between '2020-08-01' AND '2020-09-01'
        ) AS `sales`