Вывод для моего запроса возвращает дубликат DB_NAME. Мне нужно выбрать DB_NAME только один раз и для последней метки COLLECTION_TIMESTAMP. Как я могу это сделать?

#sql #oracle #oracle11g

#sql #Oracle #oracle11g

Вопрос:

Мне нужно выбрать DB_NAME только один раз и для последней метки collection_timestamp. Как я могу это сделать?

 WITH temp AS
(SELECT DISTINCT a.collection_timestamp,
round((a.value / 60),0)lag_min,
substr(b.target_name,1,instr(b.target_name,'.',1,1)- 1)db_name,
b.PROPERTY_VALUE DataGuardStatus,
c.PROPERTY_VALUE OpenMode
FROM sysman.mgmt_metrics_raw a,
SYSMAN.MGMT$TARGET_PROPERTIES b,
SYSMAN.MGMT$TARGET_PROPERTIES c
WHERE a.metric_guid = '52D41523BFF32FD2E50DB55B9E730E68'
AND round((a.value / 60),0)> 5
AND a.target_guid = b.target_guid AND b.target_guid=c.target_guid
AND (b.target_name LIKE '%opp%' OR b.target_name LIKE '%ewp%')
AND b.PROPERTY_NAME='DataGuardStatus'
AND c.PROPERTY_NAME='OpenMode'
AND a.collection_timestamp > SYSDATE - 1
)
select collection_timestamp,
lag_min,
db_name,
DataGuardStatus,
OpenMode
from(
select collection_timestamp,
lag_min,
db_name,
DataGuardStatus,
OpenMode,
max(lag_min) over (partition by db_name) max_lag_min
from temp order by collection_timestamp asc
)
where lag_min = max_lag_min
  

Актуально

 COLLECTION_TIMESTAM    LAG_MIN DB_NAME    DATAGUARDSTATUS      OPENMODE
------------------- ---------- ---------- -------------------- ------------------------------
2019-04-08 12:17:36          6 opp392b    Physical Standby     MOUNTED
2019-04-08 12:20:06         11 opp392     Physical Standby     READ ONLY WITH APPLY
2019-04-08 12:34:59         11 opp392     Physical Standby     READ ONLY WITH APPLY
2019-04-08 23:20:27         74 opp302a    Physical Standby     MOUNTED
2019-04-09 00:34:46         29 opp231a    Physical Standby     MOUNTED
2019-04-09 00:48:49         31 opp413a    Physical Standby     READ ONLY WITH APPLY
2019-04-09 00:58:55         31 opp413a    Physical Standby     READ ONLY WITH APPLY
2019-04-09 01:13:49         31 opp413a    Physical Standby     READ ONLY WITH APPLY
2019-04-09 01:55:58        120 ewp2c      Physical Standby     READ ONLY WITH APPLY
  

Ожидается

 COLLECTION_TIMESTAM    LAG_MIN DB_NAME    DATAGUARDSTATUS      OPENMODE
------------------- ---------- ---------- -------------------- ------------------------------
2019-04-08 12:17:36          6 opp392b    Physical Standby     MOUNTED
2019-04-08 12:34:59         11 opp392     Physical Standby     READ ONLY WITH APPLY
2019-04-08 23:20:27         74 opp302a    Physical Standby     MOUNTED
2019-04-09 00:34:46         29 opp231a    Physical Standby     MOUNTED
2019-04-09 01:13:49         31 opp413a    Physical Standby     READ ONLY WITH APPLY
2019-04-09 01:55:58        120 ewp2c      Physical Standby     READ ONLY WITH APPLY
  

Ответ №1:

Кажется, вы хотите получить последнюю временную метку. Если это так, посмотрите, поможет ли это:

 <snip>
SELECT MAX(collection_timestamp) collection_timestamp,         --> this ...
       lag_min,
       db_name,
       dataguardstatus,
       openmode
FROM(SELECT collection_timestamp,
            lag_min,
            db_name,
            dataguardstatus,
            openmode,
            MAX(lag_min)OVER(
              PARTITION BY db_name
            )max_lag_min
     FROM temp
     ORDER BY collection_timestamp ASC)
WHERE lag_min = max_lag_min
GROUP BY lag_min,                                 --> ... requires GROUP BY as well
         db_name,
         dataguardstatus,
         openmode;