我需要帮助,我需要根据系统行计算当前事件的上一个结束日期和开始日期之间的分钟差异。< / p>
这是表格:
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的两个系统记录没有“结束日期”的先前记录。减法:
id | system | diff_min |
---------------------------------
2 | system 1 | 0 |
---------------------------------
3 | system 1 | 1380 |
---------------------------------
5 | system 1 | 1620 |
---------------------------------
6 | system 2 | 0 |
---------------------------------
7 | system 2 | 1800 |
---------------------------------
答案 0 :(得分:0)
你可以使用两个内置函数来实现这个目的
select id, system, TIMESTAMPDIFF(MINUTE, endDate, startDate) diff_min
或
select id, system, TIMEDIFF(endDate, startDate)/60 as diff_min
答案 1 :(得分:0)
select id,system,TIMESTAMPDIFF(MINUTE,end,start) as diff_min from table_name;
答案 2 :(得分:0)
以下查询执行此操作:
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
请忽略我的查询结果集中的最后两列