简体   繁体   English

如何获取MySQL中连续数据的第一条和最后一条记录?

[英]How to get the first and last record for consecutive data in MySQL?

I was investigating the execution times for different kinds of the program I ran. 我正在调查我运行的各种程序的执行时间。 First, I've got a result from one query, like: 首先,我从一个查询中得到了一个结果,例如:

+------------+---------------------+
+ program    + start_time          +
+------------+---------------------+
| aaa        | 2015-04-01 22:18:13 |
| aaa        | 2015-04-01 22:18:19 |
| aaa        | 2015-04-01 22:18:35 |
| aaa        | 2015-04-01 22:29:18 |
| bbb        | 2015-04-01 22:29:32 |
| bbb        | 2015-04-01 22:31:38 |
| bbb        | 2015-04-01 22:41:11 |
| ccc        | 2015-04-01 22:41:20 |
| ccc        | 2015-04-01 22:42:01 |
| ccc        | 2015-04-01 23:04:05 |
| ccc        | 2015-04-01 23:04:13 |
| aaa        | 2015-04-01 23:06:06 |
| aaa        | 2015-04-01 23:22:40 |
| aaa        | 2015-04-01 23:23:27 |
| aaa        | 2015-04-01 23:24:15 |
| ddd        | 2015-04-01 23:36:15 |
| ddd        | 2015-04-01 23:36:17 |
| ddd        | 2015-04-01 23:36:21 |
+------------+---------------------+

It shows like this way because each program has different options to test, so for each option, there's a start time for this test. 之所以这样显示,是因为每个程序都有不同的测试选项,因此对于每个选项,都有一个开始测试的时间。 Don't worry about the execution time, I just want to know the rough time for each flagset. 不用担心执行时间,我只想知道每个标志集的粗略时间。 From this table, we can see, program aaa has two sets, first at the beginning, second's before ddd. 从该表中可以看到,程序aaa有两套,第一套在开始,第二套在ddd之前。 Each flagset should be calculated independently. 每个标志集应独立计算。

I want to calculate the time for each consecutive program, in a simple way, I want to get the first record time and last record time for the consecutive part, like: 我想以一种简单的方式计算每个连续程序的时间,我想获得连续部分的第一个记录时间和最后一个记录时间,例如:

+------------+---------------------+
+ program    + start_time          +
+------------+---------------------+
| aaa        | 2015-04-01 22:18:13 |
| aaa        | 2015-04-01 22:29:18 |
| bbb        | 2015-04-01 22:29:32 |
| bbb        | 2015-04-01 22:41:11 |
| ccc        | 2015-04-01 22:41:20 |
| ccc        | 2015-04-01 23:04:13 |
| aaa        | 2015-04-01 23:06:06 |
| aaa        | 2015-04-01 23:24:15 |
| ddd        | 2015-04-01 23:36:15 |
| ddd        | 2015-04-01 23:36:21 |
+------------+---------------------+

The previous solution would be good enough. 先前的解决方案就足够了。 But ideally, it's better to know the exact time interval for each program, so the ideal result for this problem would be the last record time - first record time, like: 但理想情况下,最好了解每个程序的确切时间间隔,因此,此问题的理想结果是最后记录时间-第一次记录时间,例如:

+------------+---------------+
+ program    + time_interval +
+------------+---------------+
| aaa        | 00:11:15      |
| bbb        | 00:11:39      |
| ccc        | 00:22:53      |
| aaa        | 00:18:09      |
| ddd        | 00:00:06      |
+------------+---------------+

Well, this is not a assignment, but I'm kinda newbie for MySQL. 好吧,这不是一项任务,但是我是MySQL的新手。 Any help would be highly appreciated! 任何帮助将不胜感激!

You can enumerate the groups using variables, and then aggregate to get what you want: 您可以使用变量枚举组,然后进行汇总以获得所需的内容:

select program, min(start_time), max(start_time)
from (select r.*,
             (@rn := if(@p = program, @rn,
                        if(@p := program, @rn + 1, @rn + 1)
                       )
             ) as rn
      from result r cross join
           (select @p = '', @rn := 0) vars
      order by program, start_time
     ) r
group by program, rn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM