我有一个如下所示的数据集
----------------------------------------------------
| NodeID | Status | Time |
----------------------------------------------------
| 101 | Up |2016-04-27 10:00:35 |
----------------------------------------------------
| 101 | Down |2016-04-27 10:43:12 |
----------------------------------------------------
| 101 | Up |2016-04-27 11:05:48 |
----------------------------------------------------
| 156 | Up |2016-04-27 08:17:10 |
----------------------------------------------------
| 156 | Up |2016-04-27 08:56:30 |
----------------------------------------------------
| 156 | Down |2016-04-27 09:06:12 |
----------------------------------------------------
我正在尝试获取可用性(每个应用程序启动时的时间%)。 该表不能具有自动增量字段,因为将使用预定作业连续删除记录。
我尝试使用行号和自联接来获取每行的下一条记录的时差:
SELECT A.NodeId, A.Status, TIMESTAMPDIFF(MINUTE,A.time,B.time) AS timedifference
FROM (
SELECT @row_number:=@row_number+1 AS row_number,nodeId, status, time FROM sw_node_status,
(SELECT @row_number:=0) AS t) as A
INNER JOIN
(
SELECT @row_number:=@row_number+1 AS row_number,nodeId, status, time FROM sw_node_status,
(SELECT @row_number:=0) AS t
) as B ON B.row_number = (A.row_number + 1 )
ORDER BY A.NodeId ASC
但是需要很长时间才能运行并且只给我一条记录。 预期的输出为:
| NodeID | Status | Time | Duration (mins
----------------------------------------------------------------
| 101 | Up |2016-04-27 10:00:35 | 43
----------------------------------------------------------------
| 101 | Down |2016-04-27 10:43:12 | 23
----------------------------------------------------------------
| 101 | Up |2016-04-27 11:05:48 | 0
----------------------------------------------------------------
| 156 | Up |2016-04-27 08:17:10 | 39
----------------------------------------------------------------
| 156 | Up |2016-04-27 08:56:30 | 10
----------------------------------------------------------------
| 156 | Down |2016-04-27 09:06:12 | 0
----------------------------------------------------------------
答案 0 :(得分:0)
SELECT @prev_time:=time,
nodeId, status, time,
TIMESTAMPDIFF(MINUTE,A.time,@prev_time) AS timedifference
FROM sw_node_status, (SELECT @prev_time:=some_initial_time) as init