Mysql 计算系统停机记录的上一个结束时间和当前开始时间之间的分钟数
Mysql Calculate minutes between previous end time and current start for system downtime records
我需要帮助,我需要计算当前事件的前一个结束日期和开始日期之间的分钟差, 根据系统排。
这是 table:
id |system |start |end |
------------------------------------------------------------
2 | system 1 | 2016-01-01 12:00:00 | 2016-01-01 13:00:00 |
------------------------------------------------------------
3 | system 1 | 2016-01-02 11:00:00 | 2016-01-02 12:00:00 |
------------------------------------------------------------
5 | system 1 | 2016-01-03 15:00:00 | 2016-01-03 16:00:00 |
------------------------------------------------------------
6 | system 2 | 2016-01-01 10:00:00 | 2016-01-01 11:00:00 |
------------------------------------------------------------
7 | system 2 | 2016-01-02 17:00:00 | 2016-01-02 18:00:00 |
这是结果:
ID为2和6的两条系统记录没有'end date'的先行记录做减法:
id | system | diff_min |
---------------------------------
2 | system 1 | 0 |
---------------------------------
3 | system 1 | 1380 |
---------------------------------
5 | system 1 | 1620 |
---------------------------------
6 | system 2 | 0 |
---------------------------------
7 | system 2 | 1800 |
---------------------------------
您可以使用两个内置函数实现此目的
select id, system, TIMESTAMPDIFF(MINUTE, endDate, startDate) diff_min
或
select id, system, TIMEDIFF(endDate, startDate)/60 as diff_min
select id,system,TIMESTAMPDIFF(MINUTE,end,start) as diff_min from table_name;
以下查询执行此操作:
SELECT
id,
system,
IF (@previousSystem = system, TIMESTAMPDIFF(MINUTE ,@previousEndTime,start),
@previousEndTime := 0) diff_min,
@previousSystem := system,
@previousEndTime := end
FROM
system_table,
(
SELECT
@previousSystem := NULL,
@previousEndTime := '0000-00-00 00:00:00'
) var
ORDER BY system, id;
输出:
运行 上面对给定数据的查询你将得到如下输出:
id system diff_min
2 system 1 0
3 system 1 1320
5 system 1 1620
6 system 2 0
7 system 2 1800
请忽略我查询结果集中的最后两列
我需要帮助,我需要计算当前事件的前一个结束日期和开始日期之间的分钟差, 根据系统排。
这是 table:
id |system |start |end |
------------------------------------------------------------
2 | system 1 | 2016-01-01 12:00:00 | 2016-01-01 13:00:00 |
------------------------------------------------------------
3 | system 1 | 2016-01-02 11:00:00 | 2016-01-02 12:00:00 |
------------------------------------------------------------
5 | system 1 | 2016-01-03 15:00:00 | 2016-01-03 16:00:00 |
------------------------------------------------------------
6 | system 2 | 2016-01-01 10:00:00 | 2016-01-01 11:00:00 |
------------------------------------------------------------
7 | system 2 | 2016-01-02 17:00:00 | 2016-01-02 18:00:00 |
这是结果:
ID为2和6的两条系统记录没有'end date'的先行记录做减法:
id | system | diff_min |
---------------------------------
2 | system 1 | 0 |
---------------------------------
3 | system 1 | 1380 |
---------------------------------
5 | system 1 | 1620 |
---------------------------------
6 | system 2 | 0 |
---------------------------------
7 | system 2 | 1800 |
---------------------------------
您可以使用两个内置函数实现此目的
select id, system, TIMESTAMPDIFF(MINUTE, endDate, startDate) diff_min
或
select id, system, TIMEDIFF(endDate, startDate)/60 as diff_min
select id,system,TIMESTAMPDIFF(MINUTE,end,start) as diff_min from table_name;
以下查询执行此操作:
SELECT
id,
system,
IF (@previousSystem = system, TIMESTAMPDIFF(MINUTE ,@previousEndTime,start),
@previousEndTime := 0) diff_min,
@previousSystem := system,
@previousEndTime := end
FROM
system_table,
(
SELECT
@previousSystem := NULL,
@previousEndTime := '0000-00-00 00:00:00'
) var
ORDER BY system, id;
输出:
运行 上面对给定数据的查询你将得到如下输出:
id system diff_min
2 system 1 0
3 system 1 1320
5 system 1 1620
6 system 2 0
7 system 2 1800
请忽略我查询结果集中的最后两列