我的查询的输出返回重复的DB_NAME。对于最新的COLLECTION_TIMESTAMP,我只需选择一次DB_NAME。我怎样才能做到这一点?

时间:2019-04-09 17:15:12

标签: sql oracle oracle11g

对于最新的collection_timestamp,我只需选择一次DB_NAME。我该怎么办?

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 个答案:

答案 0 :(得分: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;