我有以下mysql表:
Start Time End Time Duration
17:00:02 17:00:31 00:00:29
17:02:30 17:02:41 00:00:11
17:11:25 17:15:25 00:04:00
17:15:37 17:17:51 00:02:14
17:18:08 17:20:17 00:02:09
17:20:35 17:20:37 00:00:02
17:20:39 17:21:25 00:00:46
17:21:33 17:22:02 00:00:29
每当持续时间少于2分钟时,我需要合并行:这应该如下所示:
Start Time End Time Duration
17:00:02 17:15:25 00:04:40
17:15:37 17:17:51 00:02:14
17:18:08 17:20:17 00:02:09
17:20:35 17:22:02 00:01:17
怎么可以这样做? 您可以假设开始时间/结束时间/持续时间已经在unix时间戳中。
答案 0 :(得分:0)
您可以通过为每个行分配一个组然后按组进行聚合来完成此操作。在这种情况下,该组是数据中较早出现持续时间超过2分钟的记录的次数:
select min(StartTime) as StartTime, max(EndTime) as EndTime, sum(Duration) as Duration
from (select t.*,
(select count(*)
from table t2
where t2.StartTime < t.StartTime and
t2.Duration >= '00:02:00'
) as grp
from table t
) t
group by grp;
答案 1 :(得分:0)
这在PHP中可能比MySQL更有效:
$results = array();
$curstart = null;
while ($row = $stmt->fetch_assoc()) {
if ($curstart == null) {
$curstart = $row['start_time'];
$curdur = 0;
}
$curdur += $row['duration'];
$curend = $row['end_time']
if ($row['duration'] >= 120) {
$results[] = array('start_time' => $curstart,
'end_time' => $end_time,
'duration' => $curdur);
$curstart = null;
$curdur = 0;
}
}
if ($curstart != null) { // Get last block if it didn't end with duration > 2 min
$results[] = array('start_time' => $curstart,
'end_time' => $end_time,
'duration' => $curdur);
}
答案 2 :(得分:0)
经过一番努力,这里是:
select min(start_time) start_time, max(end_time) end_time,
TIME_FORMAT(SEC_TO_TIME(sum(duration)),'%i:%s') duration
from (
select start_time, end_time, duration,
if (@dur>=120, @grp:=@grp+1, @grp) g,
if (@dur>=120, @dur:=duration, @dur:=@dur+duration) dur
from mytable a
join (select @dur:=0, @grp:=0) b) a
group by a.g;