我需要在此表中找到MTTF(平均故障时间)。
machine_type_id time machine_id event_type
275 0 97957590 0
11148 64082917976 97957590 1
11160 64508629318 97957590 0
11565 94842321864 97957590 1
11567 95269562076 97957590 0
34517 2334183699633 97957590 1
34519 2334580893038 97957590 0
其中,event_type = 0
是机器添加或打开时,event_type= 1
是机器发生故障时。 MTTF= total downtime /number of failure
。
我的SQL脚本在从正确序列中添加的时间减去时间故障时遇到问题。例如,它可以在机器添加到池中的所有时间减去重新发生的故障。虽然它应该从上次机器添加池中减去。另一个问题是将总停机时间与故障数量分开的最后阶段。
我的SQL脚本是
select o.machine_id, sum(o.timefailure- t.timeadded)/count(o.event_type) as MTTF
from
(
select machine_id, machine_type_id ,time as timeadded
from machine_events
where event_type= 0 and machine_id=97957590
) as t
INNER JOIN
(
select machine_id,machine_type_id, event_type,time as timefailure
from machine_events
where event_type = 1 and machine_id=97957590
) as o
on o.machine_id=t.machine_id
where o.timefailure- t.timeadded <> 0 and o.machine_type_id > t.machine_type_id
group by o.machine_id
我将把简单数据用于测试
--INSERT INTO VALUES ( 275, 0, 111, 0 );
--INSERT INTO VALUES ( 11148, 4, 111, 1 );
--INSERT INTO VALUES ( 11160, 7, 111, 0 );
--INSERT INTO VALUES ( 11565, 17, 111, 1 );
--INSERT INTO VALUES ( 11567, 20, 111, 0 );
--INSERT INTO VALUES ( 34517, 40, 111, 1 );
--INSERT INTO VALUES ( 34519, 50, 111, 0 );
这里的总停机时间(4 + 10 + 20)。所以MTTF= Totaldowntime/number of failure
答案 0 :(得分:1)
DROP TABLE deleteme_tbl;
CREATE TABLE deleteme_tbl
(
machine_type_id INT
, time INT
, machine_id INT
, event_type INT
);
INSERT INTO deleteme_tbl VALUES ( 275, 0, 97957590, 0 );
INSERT INTO deleteme_tbl VALUES ( 11148, 64082917976, 97957590, 1 );
INSERT INTO deleteme_tbl VALUES ( 11160, 64508629318, 97957590, 0 );
INSERT INTO deleteme_tbl VALUES ( 11565, 94842321864, 97957590, 1 );
INSERT INTO deleteme_tbl VALUES ( 11567, 95269562076, 97957590, 0 );
INSERT INTO deleteme_tbl VALUES ( 34517, 2334183699633, 97957590, 1 );
INSERT INTO deleteme_tbl VALUES ( 34519, 2334580893038, 97957590, 0 );
COMMIT;
WITH
addset
AS
(SELECT machine_type_id
, time
, machine_id
, event_type
FROM deleteme_tbl
WHERE event_type = 0),
dropset
AS
(SELECT machine_type_id
, time
, machine_id
, event_type
FROM deleteme_tbl
WHERE event_type = 1),
finalset
AS
(SELECT addset.*
, (SELECT MIN (time)
FROM dropset
WHERE dropset.machine_id = addset.machine_id
AND dropset.time > addset.time)
AS time_off
FROM addset)
SELECT machine_id, SUM (time_off - time) / COUNT (*) mttf
FROM finalset
GROUP BY machine_id