使用完全不同的方式做的另一个答案: -
SELECT MAX(@Counter) AS EventCount -- Get the max counter
FROM (SELECT @Counter:=@Counter + IF(status = 'OK' AND @PrevStatus = 1, 1, 0), -- If it is an OK record and the prev status was not an OK then add 1 to the counter
@PrevStatus:=CASE
WHEN status = 'OK' THEN @PrevStatus := 2 -- An OK status so save as a prev status of 2
WHEN status != 'OK' AND @PrevStatus != 0 THEN @PrevStatus := 1 -- A non OK status but when there has been a previous OK status
ELSE @PrevStatus:=0 -- Set the prev status to 0, ie, for a record where there is no previous OK status
END
FROM (SELECT * FROM historical ORDER BY TimeStamp) a
CROSS JOIN (SELECT @Counter:=0, @PrevStatus := 0) b -- Initialise counter and store of prev status.
)c
这是使用用户变量。它有一个子选择来以正确的顺序取回记录,然后使用用户变量来存储先前状态的代码。从 0 开始,当它找到 OK 状态时,它将先前的状态设置为 2。如果它发现不是 OK 的状态,则它将 prev 状态设置为 1,但仅当 prev 状态不为 0 时(即,它已经发现状态OK)。在存储上一个状态码之前,如果当前状态是OK,并且上一个状态码是1,那么计数器加1,否则加0(即什么都不加)
然后它只是在外面有一个选择来选择计数器的最大值。
似乎工作,但几乎不可读!
编辑 - 处理多个 id
SELECT id, MAX(aCounter) AS EventCount -- Get the max counter for each id
FROM (SELECT id,
@PrevStatus:= IF(@Previd = id, @PrevStatus, 0), -- If the id has changed then set the store of previous status to 0
status,
@Counter:=IF(@Previd = id, @Counter + IF(status = 'OK' AND @PrevStatus = 1, 1, 0), 0) AS aCounter, -- If it is an OK record and the prev status was not an OK and was for the same id then add 1 to the counter
@PrevStatus:=CASE
WHEN status = 'OK' THEN @PrevStatus := 2 -- An OK status so save as a prev status of 2
WHEN status != 'OK' AND @PrevStatus != 0 THEN @PrevStatus := 1 -- A non OK status but when there has been a previous OK status
ELSE @PrevStatus:=0 -- Set the prev status to 0, ie, for a record where there is no previous OK status
END,
@Previd := id
FROM (SELECT * FROM historical ORDER BY id, TimeStamp) a
CROSS JOIN (SELECT @Counter:=0, @PrevStatus := 0, @Previd := 0) b
)c
GROUP BY id -- Group by clause to allow the selection of the max counter per id
这更不可读!
另一种选择,再次使用用户变量生成序列号:-
SELECT Sub1.id, COUNT(DISTINCT Sub1.aCounter) -- Count the number of distinct Sub1 records found for an id (without the distinct counter it would count all the recods between OK status records)
FROM (
SELECT id,
`TimeStamp`,
@Counter1:=IF(@Previd1 = id, @Counter1 + 1, 0) AS aCounter, -- Counter for this status within id
@Previd1 := id -- Store the id, used to determine if the id has changed and so whether to start the counters at 0 again
FROM (SELECT * FROM historical WHERE status = 'OK' ORDER BY id, `TimeStamp`) a -- Just get the OK status records, in id / timestamp order
CROSS JOIN (SELECT @Counter1:=0, @Previd1 := 0) b -- Initialise the user variables.
) Sub1
INNER JOIN (SELECT id,
`TimeStamp`,
@Counter2:=IF(@Previd2 = id, @Counter2 + 1, 0) AS aCounter,-- Counter for this status within id
@Previd2 := id-- Store the id, used to determine if the id has changed and so whether to start the counters at 0 again
FROM (SELECT * FROM historical WHERE status = 'OK' ORDER BY id, `TimeStamp`) a -- Just get the OK status records, in id / timestamp order
CROSS JOIN (SELECT @Counter2:=0, @Previd2 := 0) b -- Initialise the user variables.
) Sub2
ON Sub1.id = Sub2.id -- Join the 2 subselects based on the id
AND Sub1.aCounter + 1 = Sub2.aCounter -- and also the counter. So Sub1 is an OK status, while Sub2 the the next OK status for that id
INNER JOIN historical Sub3 -- Join back against historical
ON Sub1.id = Sub3.id -- on the matching id
AND Sub1.`TimeStamp` < Sub3.`TimeStamp` -- and where the timestamp is greater than the timestamp in the Sub1 OK record
AND Sub2.`TimeStamp` > Sub3.`TimeStamp` -- and where the timestamp is less than the timestamp in the Sub2 OK record
GROUP BY Sub1.id -- Group by the Sub1 id
这是为了状态 OK 记录两次抓取表,每次添加一个序列号并匹配 id 匹配的位置,并且第二个副本上的序列号比第一个大 1(即,它正在查找每个 OK 和好的,紧随其后)。然后将其与 id 匹配且时间戳位于 2 条 OK 记录之间的表连接起来。然后计算每个 id 的第一个计数器的不同出现次数。
这应该更具可读性。