每月 Oracle 停机时间(以分钟为单位)
Oracle downtime in minutes per month
这是用Oracle 11g或12c写的...
您好,
我试图将此数据整理成过去 12 个月的停机时间,以这种方式整理以支持图表。
在下面的 Oracle SQL 查询中,注释查询 "M" 显示了如何每月输入一条记录。查询 "M" 显示了我需要的最终形状,除了我需要将停机时间分钟数放入每个月度箱中。
然而,数据是由查询 "D" 提供的,我为了这个例子而模拟了它。当然,更好的示例可能会显示更多的边缘情况,例如跨越一个月的第一天等。但是这个示例就足够了。
WITH
/*
M AS (
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), -1 * LEVEL) AS MO
, 0 AS DOWNTIME FROM DUAL
CONNECT BY LEVEL <= 12),
*/
D AS ( -- data simulating a downtime day
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('9/25/2015 15:12:47', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
385392 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Down' AS AVAILABILITY_STATUS,
TO_DATE ('11/25/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
1440 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('11/26/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
200122 AS MINUTES
FROM DUAL)
SELECT *
FROM D
PIVOT
(SUM (MINUTES)
AS TIME
FOR AVAILABILITY_STATUS
IN ('Target Up' AS UP,
'Target Down' AS DOWN,
'Blackout' AS BLACKOUT));
现在的结果是这样的:
SINCE UP_TIME DOWN_TIME BLACKOUT_TIME
9/25/2015 3:12:47 PM 385,392
11/25/2015 3:12:00 PM 1,440
11/26/2015 3:12:00 PM 200,122
但我需要它看起来像这样:
MO DOWN_MINUTES
3/1/2016 0
2/1/2016 0
1/1/2016 0
12/1/2015 0
11/1/2015 1440
10/1/2015 0
9/1/2015 0
8/1/2015 0
7/1/2015 0
6/1/2015 0
5/1/2015 0
4/1/2015 0
P.S.:为了组的利益,查询"M"中的实际宕机是由以下查询生成的,从Oracle读取企业经理:
WITH X
AS (SELECT START_TIMESTAMP,
NVL (END_TIMESTAMP, SYSDATE) AS END_TIMESTAMP,
AVAILABILITY_STATUS,
TRUNC (
(NVL (END_TIMESTAMP, SYSDATE) - START_TIMESTAMP) * 24 * 60)
MINUTES
FROM MGMT$AVAILABILITY_HISTORY
WHERE AVAILABILITY_STATUS IN
('Target Down', 'Target Up', 'Blackout')
)
SELECT AVAILABILITY_STATUS,
MIN (START_TIMESTAMP) AS SINCE,
SUM (MINUTES) AS MINUTES
FROM X
GROUP BY AVAILABILITY_STATUS
ORDER BY MIN (START_TIMESTAMP);
提前谢谢你。我已经为此苦苦思索了好几天,是时候寻求帮助了。
试试这个:
WITH
M AS (
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), -1 * LEVEL) AS MO
, 0 AS DOWNTIME FROM DUAL
CONNECT BY LEVEL <= 12),
D AS ( -- data simulating a downtime day
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('9/25/2015 15:12:47', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
385392 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Down' AS AVAILABILITY_STATUS,
TO_DATE ('11/25/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
1440 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('11/26/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
200122 AS MINUTES
FROM DUAL)
SELECT M.mo, Nvl(s, 0) AS minutes
FROM M
LEFT JOIN ( SELECT TRUNC (SINCE, 'MONTH') AS D, SUM (MINUTES) AS S
FROM D
WHERE AVAILABILITY_STATUS = 'Target Down'
GROUP BY TRUNC (SINCE, 'MONTH')) GR
ON GR.D = M.MO
ORDER BY 1;
这是用Oracle 11g或12c写的...
您好,
我试图将此数据整理成过去 12 个月的停机时间,以这种方式整理以支持图表。
在下面的 Oracle SQL 查询中,注释查询 "M" 显示了如何每月输入一条记录。查询 "M" 显示了我需要的最终形状,除了我需要将停机时间分钟数放入每个月度箱中。
然而,数据是由查询 "D" 提供的,我为了这个例子而模拟了它。当然,更好的示例可能会显示更多的边缘情况,例如跨越一个月的第一天等。但是这个示例就足够了。
WITH
/*
M AS (
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), -1 * LEVEL) AS MO
, 0 AS DOWNTIME FROM DUAL
CONNECT BY LEVEL <= 12),
*/
D AS ( -- data simulating a downtime day
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('9/25/2015 15:12:47', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
385392 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Down' AS AVAILABILITY_STATUS,
TO_DATE ('11/25/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
1440 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('11/26/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
200122 AS MINUTES
FROM DUAL)
SELECT *
FROM D
PIVOT
(SUM (MINUTES)
AS TIME
FOR AVAILABILITY_STATUS
IN ('Target Up' AS UP,
'Target Down' AS DOWN,
'Blackout' AS BLACKOUT));
现在的结果是这样的:
SINCE UP_TIME DOWN_TIME BLACKOUT_TIME 9/25/2015 3:12:47 PM 385,392 11/25/2015 3:12:00 PM 1,440 11/26/2015 3:12:00 PM 200,122
但我需要它看起来像这样:
MO DOWN_MINUTES 3/1/2016 0 2/1/2016 0 1/1/2016 0 12/1/2015 0 11/1/2015 1440 10/1/2015 0 9/1/2015 0 8/1/2015 0 7/1/2015 0 6/1/2015 0 5/1/2015 0 4/1/2015 0
P.S.:为了组的利益,查询"M"中的实际宕机是由以下查询生成的,从Oracle读取企业经理:
WITH X
AS (SELECT START_TIMESTAMP,
NVL (END_TIMESTAMP, SYSDATE) AS END_TIMESTAMP,
AVAILABILITY_STATUS,
TRUNC (
(NVL (END_TIMESTAMP, SYSDATE) - START_TIMESTAMP) * 24 * 60)
MINUTES
FROM MGMT$AVAILABILITY_HISTORY
WHERE AVAILABILITY_STATUS IN
('Target Down', 'Target Up', 'Blackout')
)
SELECT AVAILABILITY_STATUS,
MIN (START_TIMESTAMP) AS SINCE,
SUM (MINUTES) AS MINUTES
FROM X
GROUP BY AVAILABILITY_STATUS
ORDER BY MIN (START_TIMESTAMP);
提前谢谢你。我已经为此苦苦思索了好几天,是时候寻求帮助了。
试试这个:
WITH
M AS (
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), -1 * LEVEL) AS MO
, 0 AS DOWNTIME FROM DUAL
CONNECT BY LEVEL <= 12),
D AS ( -- data simulating a downtime day
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('9/25/2015 15:12:47', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
385392 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Down' AS AVAILABILITY_STATUS,
TO_DATE ('11/25/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
1440 AS MINUTES
FROM DUAL
UNION ALL
SELECT 'Target Up' AS AVAILABILITY_STATUS,
TO_DATE ('11/26/2015 15:12:00', 'MM/DD/YYYY HH24:MI:SS')
AS SINCE,
200122 AS MINUTES
FROM DUAL)
SELECT M.mo, Nvl(s, 0) AS minutes
FROM M
LEFT JOIN ( SELECT TRUNC (SINCE, 'MONTH') AS D, SUM (MINUTES) AS S
FROM D
WHERE AVAILABILITY_STATUS = 'Target Down'
GROUP BY TRUNC (SINCE, 'MONTH')) GR
ON GR.D = M.MO
ORDER BY 1;