0

所以我有一个每 5 分钟记录一次的事件日志,所以我的日志看起来像这样:

OK
Event1
Event1
Event1
OK
Event1
OK
Event1
Event1
Event1
OK

在这种情况下,我将有 3 个“Event1”实例,因为它在返回该状态的时间段之间有一个“OK”时间段。

有没有一些不错的方法可以通过 mySql 处理这个问题?(注意,除了 Event1 / OK 之外,还有其他状态经常出现)

实际的 Sql 结构如下所示:

-Historical
    --CID //Unique Identifier, INT, AI
    --ID  //Unique Identifier for LOCATION, INT
    --LOCATION //Unique Identifier for Location, this is the site name, VarChar
    --STATUS //Pulled from Software event logger, VarChar
    --TIME //Pulled from Software event logger, DateTime
4

3 回答 3

1

使用完全不同的方式做的另一个答案: -

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 的第一个计数器的不同出现次数。

这应该更具可读性。

于 2013-06-25T17:00:32.020 回答
0

快速尝试,我觉得我错过了一种更好的方法来做到这一点,但我认为这会奏效。

SELECT COUNT(*)
FROM
(
    SELECT DISTINCT a.time, b.time
    FROM Historical a
    INNER JOIN Historical b
    ON a.time < b.time
    AND a.status = 'OK'
    AND b.status = 'OK'
    INNER JOIN Historical c
    ON a.time < c.time
    AND c.time < b.time
    AND c.status = 'Event1'
    LEFT OUTER JOIN Historical d
    ON a.time < d.time
    AND d.time < b.time
    AND d.status = 'OK'
    WHERE d.cid IS NULL
) Sub1

反复将表与自身相连接。别名 a 和 b 应该用于 OK 事件,c 用于这些日期之间的任何 Event1 事件。别名 d 正在寻找 a 和 b 之间的 OK 事件,如果找到,则将记录删除到 WHERE 子句中。

然后使用 DISTINCT 删除重复项。然后计算结果。

可能它可以简化为如下所示(尽管如果这样做最好将日期转换为选择中的字符)

SELECT COUNT(DISTINCT CONCAT(a.time, b.time))
FROM Historical a
INNER JOIN Historical b
ON a.time < b.time
AND a.status = 'OK'
AND b.status = 'OK'
INNER JOIN Historical c
ON a.time < c.time
AND c.time < b.time
AND c.status = 'Event1'
LEFT OUTER JOIN Historical d
ON a.time < d.time
AND d.time < b.time
AND d.status = 'OK'
WHERE d.cid IS NULL
于 2013-06-25T13:57:38.657 回答
0

您想计算的似乎是上一条记录为 时的事件实例OK。你用一个相关的子查询来识别这些,然后总结得到数字:

select status, count(*)
from (select h.*,
             (select h2.status
              from historical h2
              where h2.time < h.time
              order by h2.time desc
              limit 1
             ) as prevStatus
      from historical h
     ) h
where status <> 'OK' and (prevStatus = 'OK' or prevStatus is NULL)
group by status;

不清楚哪一列包含值OKEvent1。我猜是status。我也不知道扮演什么角色location,但这至少应该让你开始。

于 2013-06-25T13:57:45.390 回答