MYSQL 使用计算字段使用 Group By 或从先前的 MAX 返回值

问题描述 投票:0回答:1

嘿 stackoverflow 社区,

我目前确实需要 MYSQL 查询的提示。

在我的数据库中,存储了不同“标签”的计数器。

目前我已经获得了当月的最小值和最大值。我的挑战是,我需要上个月的最后一个 MAX。

有人有想法吗?

SELECT 
  CONCAT(YEAR(timestamp),"-",LPAD(MONTH(timestamp), 2, '0')) as DATE,
  MIN(counter) as counterMIN,
  MAX(counter) as counterMAX,
  MIN(timestamp) as timestampMIN,
  MAX(timestamp) as timestampMAX,
  tag
FROM `seller` 
WHERE tag LIKE "SYS%"
GROUP BY CONCAT(YEAR(timestamp),"-",MONTH(timestamp)),`tag`
ORDER BY CONCAT(YEAR(timestamp),"-",LPAD(MONTH(timestamp), 2, '0'))
日期 标签 计数器MIN 计数器MAX
2023-12 SYS001 46 312
2024-01 SYS001 476 807

我的挑战是我需要 counterMAX 值是上个月的值。因此,我想使用子查询,但看起来我无法使用计算字段执行子查询。

日期 标签 计数器MIN 计数器MAX counterMAX上个月
2023-12 SYS001 46 312 0
2024-01 SYS001 476 807 312

MYSQL数据

CREATE TABLE `seller` (
  `counter` decimal(10,0) NOT NULL,
  `tag` varchar(6) NOT NULL,
  `timestamp` timestamp NOT NULL
);
INSERT INTO `seller` (`counter`, `tag`, `timestamp`) VALUES
(46, 'SYS001', '2023-12-10 08:00:00'),
(199, 'SYS001', '2023-12-13 08:00:00'),
(312, 'SYS001', '2023-12-24 08:00:00'),
(476, 'SYS001', '2024-01-08 08:00:00'),
(493, 'SYS001', '2024-01-12 08:00:00'),
(618, 'SYS001', '2024-01-18 08:00:00'),
(807, 'SYS001', '2024-01-28 08:00:00'),
(858, 'SYS001', '2024-02-06 08:00:00'),
(961, 'SYS001', '2024-02-11 08:00:00'),
(1152, 'SYS001', '2024-02-26 08:00:00'),
(1281, 'SYS001', '2024-03-03 08:00:00'),
(1348, 'SYS001', '2024-03-07 08:00:00'),
(1397, 'SYS001', '2024-03-21 08:00:00'),
(1556, 'SYS001', '2024-04-06 07:00:00'),
(1625, 'SYS001', '2024-04-17 07:00:00'),
(172, 'SYS002', '2023-12-10 08:00:00'),
(334, 'SYS002', '2023-12-21 08:00:00'),
(519, 'SYS002', '2023-12-27 08:00:00'),
(630, 'SYS002', '2024-01-14 08:00:00'),
(790, 'SYS002', '2024-01-17 08:00:00'),
(884, 'SYS002', '2024-01-30 08:00:00'),
(928, 'SYS002', '2024-02-12 08:00:00'),
(938, 'SYS002', '2024-02-15 08:00:00'),
(1103, 'SYS002', '2024-02-23 08:00:00'),
(1289, 'SYS002', '2024-02-25 08:00:00'),
(1361, 'SYS002', '2024-02-27 08:00:00'),
(1553, 'SYS002', '2024-03-07 08:00:00'),
(1597, 'SYS002', '2024-03-15 08:00:00'),
(1604, 'SYS002', '2024-03-22 08:00:00'),
(1762, 'SYS002', '2024-03-29 08:00:00');

提示查询还可以解析上个月的最大值

mysql
1个回答
0
投票
WITH cte AS (
  SELECT DATE_FORMAT(`timestamp`, '%Y-%m') as `DATE`,
         MIN(counter) as counterMIN,
         MAX(counter) as counterMAX,
         MIN(`timestamp`) as timestampMIN,
         MAX(`timestamp`) as timestampMAX,
         tag
  FROM `seller` 
  WHERE tag LIKE "SYS%"
  GROUP BY `DATE`,`tag`
--  ORDER BY `DATE`
)
SELECT *, LAG(counterMAX) OVER (PARTITION BY tag ORDER BY `DATE`) counterMAXPrevMonth
FROM cte
ORDER BY tag, `DATE`
日期 计数器MIN 计数器MAX 时间戳MIN 时间戳最大 标签 counterMAX上个月
2023-12 46 312 2023-12-10 08:00:00 2023-12-24 08:00:00 SYS001
2024-01 476 807 2024-01-08 08:00:00 2024-01-28 08:00:00 SYS001 312
2024-02 858 1152 2024-02-06 08:00:00 2024-02-26 08:00:00 SYS001 807
2024-03 1281 1397 2024-03-03 08:00:00 2024-03-21 08:00:00 SYS001 1152
2024-04 1556 1625 2024-04-06 07:00:00 2024-04-17 07:00:00 SYS001 1397
2023-12 172 519 2023-12-10 08:00:00 2023-12-27 08:00:00 SYS002
2024-01 630 884 2024-01-14 08:00:00 2024-01-30 08:00:00 SYS002 519
2024-02 928 1361 2024-02-12 08:00:00 2024-02-27 08:00:00 SYS002 884
2024-03 1553 1762 2024-03-07 08:00:00 2024-03-29 08:00:00 SYS002 1361

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.