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

Group by a variable range in mysql / MariaDB(在 mysql/MariaDB 中按变量范围分组)
本文介绍了在 mysql/MariaDB 中按变量范围分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个历史记录,我想总结一些条目.间隔应为 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;  

请检查并说出什么问题

这篇关于在 mysql/MariaDB 中按变量范围分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)