我只需要查找具有最大LAG_MIN

时间:2019-03-31 16:17:09

标签: sql oracle oracle11g

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
FROM sysman.mgmt_metrics_raw a,
     sysman.mgmt$agents_monitoring_targets b
WHERE a.metric_guid = '26372D42E0B05538E505F330D67E8CEA'
      AND round((a.value / 60),0) > 5
      AND a.target_guid = b.target_guid
      AND b.target_name LIKE '%opp%'
      AND a.collection_timestamp > SYSDATE - 1
ORDER BY a.collection_timestamp ASC;

返回以下输出。但是我只需要查找具有最大LAG_MIN的DB_NAME。我该怎么办?

COLLECTION_TIMESTAM    LAG_MIN DB_NAME
------------------- ---------- ----------
2019-03-31 08:27:29        488 opp449a
2019-03-31 08:29:33        105 opp447a
2019-03-31 08:32:29        493 opp449a
2019-03-31 08:34:32        110 opp447a
2019-03-31 08:37:29        498 opp449a
2019-03-31 08:39:33        115 opp447a

1 个答案:

答案 0 :(得分:0)

如果您将当前查询用作CTE(我将其命名为“ temp”),则可以执行以下操作:

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
     FROM sysman.mgmt_metrics_raw a,
          sysman.mgmt$agents_monitoring_targets b
     WHERE a.metric_guid = '26372D42E0B05538E505F330D67E8CEA'
       AND round((a.value / 60),0)> 5
       AND a.target_guid = b.target_guid
       AND b.target_name LIKE '%opp%'
       AND a.collection_timestamp > SYSDATE - 1
  )
SELECT *
FROM temp
WHERE lag_min = (SELECT MAX(lag_min)
                 FROM temp
                );