【问题标题】:GROUP BY with MAX() return wrong id of the rows带有 MAX() 的 GROUP BY 返回错误的行 ID
【发布时间】:2021-04-05 03:06:12
【问题描述】:

我想在执行请求时获得每个 order_product_id 每周的最大使用容量。 WHERE 子句中的 JOIN 或 SELECT 变体不起作用,因为 max_capacity 对某些 order_product_id 重复。我的查询每周返回正确的 order_product_id 和 max_capacity,但没有返回正确的行 ID。

CREATE TABLE `capacity_log` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`date_occurred` DATETIME NOT NULL,
`ip_address` VARCHAR(255) NOT NULL DEFAULT '',
`order_product_id` INT UNSIGNED NOT NULL,
`serial` VARCHAR(255) NOT NULL DEFAULT '',
`used_capacity` BIGINT NULL DEFAULT NULL,
`aux2` INT NULL DEFAULT NULL,
`request` BLOB NULL,
`retry_count` INT NOT NULL DEFAULT '0',
`fetch_time` INT NOT NULL DEFAULT '0',
`response` BLOB NULL,
`custom_fetch_time` INT NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `user_id` (`order_product_id`))

我的查询:

SELECT  c.order_product_id, MAX(c.used_capacity) AS `max_capacity`
FROM capacity_log c
WHERE c.date_occurred < '2020-10-1' AND c.aux2 IS NULL 
GROUP BY
    YEAR(c.date_occurred), WEEK(c.date_occurred),
    c.order_product_id
    

【问题讨论】:

    标签: mysql sql datetime max greatest-n-per-group


    【解决方案1】:

    您想要整行,因此聚合不是您所追求的。相反,您需要过滤。一种选择使用子查询:

    select c.*
    from capacity_log c
    where c.id = (
        select c1.id
        from capacity_log c1
        where  
            c1.date_occurred < '2020-10-1' 
            and c1.aux2 is null
            and c1.order_product_id = c.order_product_id
            and yearweek(c1.date_occurred) = yearweek(c.date_occurred)
        order by c1.used_capacity desc limit 1
    )
    

    我们可以像这样优化子查询的where子句:

        where  
            c1.date_occurred < '2020-10-1' 
            and c1.aux2 is null
            and c1.order_product_id = c.order_product_id
            and c1.date_occurred >= c.date_occurred - interval weekday(c.date_occurred) day
            and c1.date_occurred <  c.date_occurred - interval weekday(c.date_occurred) day + interval 7 day
    

    为了提高性能,您需要在(order_product_id, aux2, date_occurred, used_capacity, id) 上建立索引。

    【讨论】:

    • 预编辑版本返回的正是我想要的,但是当我使用具有更多数据的数据库进行测试时,需要很长时间。有什么想法吗?
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 2017-10-29
    • 2021-09-06
    • 2021-08-02
    • 1970-01-01
    • 2011-01-22
    • 2013-03-25
    • 2013-08-15
    相关资源
    最近更新 更多