这是用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 Enterprise Manager中读取:
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);
提前谢谢你。我已经在这上面旋转了几天,现在是时候寻求帮助了。
答案 0 :(得分:1)
试试这个:
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;