在 mysql/MariaDB 中按变量范围分组

2022-01-15 00:00:00 group-by intervals mariadb mysql

我有一个历史记录,我想总结一些条目.间隔应为 5 秒

I have a history log and i want to summarize some entries. The interval should be 5 seconds

例如:我有一个清单

date_start          | date_end           | count | somestring
2015-09-15 12:04:09 | 2015-09-15 12:04:09| 1     | xyz
2015-09-15 12:05:09 | 2015-09-15 12:05:09| 1     | xyz
2015-09-15 12:05:10 | 2015-09-15 12:05:10| 1     | xyz
2015-09-15 12:05:11 | 2015-09-15 12:05:11| 1     | xyz
2015-09-15 12:06:09 | 2015-09-15 12:06:09| 1     | xyz

我现在想要一个类似的输出

I want now to have an output like

date_start          | date_end           | count | somestring
2015-09-15 12:04:09 | 2015-09-15 12:04:09| 1     | xyz
2015-09-15 12:05:09 | 2015-09-15 12:05:11| 3     | xyz <--
2015-09-15 12:06:09 | 2015-09-15 12:06:09| 1     | xyz

因此,如果在 5 秒间隔内有重复项,我想将其分组到一个条目中.但是如果每个最大值在 1 小时内有多个条目.相隔 5 秒,我也想计算所有这些条目.

so if there is a duplicate in a 5 sec interval I want to group it to one entry. but if there are several entries in 1 hour with each max. 5 seconds apart I want to count all of these entries too.

有人知道方法吗?我已经在这工作了好几个星期了:(

does somebody know a way? I am working at this for weeks now :(

Bernd的回答和评论:非常感谢,您的查询的输出是:谢谢@Bernd.问题是,您的查询的输出是:

answer and comment for Bernd: Thank you so much, the output of your query is: thank you @Bernd. The Problem is, the output with your query is:

+---------+---------------------+--------------+---------------------+----+---------------------+---------------------+-----------+------+
| mycount | st                  | group_number | tmp_interv          | id | start_date          | end_date            | some_text | cnt  |
+---------+---------------------+--------------+---------------------+----+---------------------+---------------------+-----------+------+
|       2 | 2015-09-14 12:00:05 | 0            | 2015-09-14 12:00:05 |  1 | 2015-09-14 12:00:00 | 2015-09-14 12:00:00 | some      |    1 |
|       4 | 2015-09-14 12:00:05 | 1            | 2015-09-14 12:01:08 |  3 | 2015-09-14 12:01:03 | 2015-09-14 12:01:03 | some      |    1 |
|       1 | 2015-09-14 12:01:08 | 2            | 2015-09-14 12:01:14 |  7 | 2015-09-14 12:01:09 | 2015-09-14 12:01:09 | some      |    1 |
+---------+---------------------+--------------+---------------------+----+---------------------+---------------------+-----------+------+

但它应该是这样的:

+---------+---------------------+--------------+---------------------+----+---------------------+---------------------+-----------+------+
| mycount | st                  | group_number | tmp_interv          | id | start_date          | end_date            | some_text | cnt  |
+---------+---------------------+--------------+---------------------+----+---------------------+---------------------+-----------+------+
|       2 | 2015-09-14 12:00:05 | 0            | 2015-09-14 12:00:05 |  1 | 2015-09-14 12:00:00 | 2015-09-14 12:00:03 | some      |    1 |
|       5 | 2015-09-14 12:00:05 | 1            | 2015-09-14 12:01:08 |  3 | 2015-09-14 12:01:03 | 2015-09-14 12:01:09 | some      |    1 |
+---------+---------------------+--------------+---------------------+----+--------------------+----------------------+-----------+------+

注意 count 和 date_end :)

be aware of the count and date_end :)

推荐答案

这是我的第一次尝试:

CREATE TABLE `dtable` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `start_date` timestamp NULL DEFAULT NULL,
  `end_date` timestamp NULL DEFAULT NULL,
  `some_text` varchar(32) DEFAULT NULL,
  `cnt` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

INSERT INTO `dtable` (`id`, `start_date`, `end_date`, `some_text`, `cnt`)
VALUES
    (1, '2015-09-14 12:00:00', '2015-09-14 12:00:00', 'some', 1),
    (2, '2015-09-14 12:00:03', '2015-09-14 12:00:03', 'some', 1),
    (3, '2015-09-14 12:01:03', '2015-09-14 12:01:03', 'some', 1),
    (4, '2015-09-14 12:01:04', '2015-09-14 12:01:03', 'some', 1),
    (5, '2015-09-14 12:01:05', '2015-09-14 12:01:03', 'some', 1),
    (6, '2015-09-14 12:01:08', '2015-09-14 12:01:08', 'some', 1),
    (7, '2015-09-14 12:01:09', '2015-09-14 12:01:09', 'some', 1);


SELECT sum(cnt) mycount, t.* FROM (
  SELECT
    @interval_end:=IF(@interval_end = 0, d.start_date + INTERVAL 5 SECOND, @interval_end ) st,
    @group_nr:= IF( d.start_date > @interval_end, @group_nr:=@group_nr+1, @group_nr ) group_number,
    @interval_end:= IF( d.start_date > @interval_end, d.start_date + INTERVAL 5 SECOND , @interval_end ) tmp_interv,
    d.* FROM dtable d,(SELECT @group_nr:=0, @interval_end:=0) tmp
) AS t
GROUP BY t.group_number;  

请检查并说出什么问题

相关文章