【发布时间】: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