简体   繁体   中英

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. 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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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