你如何从oracle表中检索最新的值

时间:2013-06-11 14:23:01

标签: sql oracle

我正在尝试根据服务器名称从oracle表中获取最新值。我有以下sql:

SELECT T."Node",T."Timestamp",T."MAX_User_CPU_Pct", T."MAX_System_CPU_Pct" 
FROM DW.KPX_CPU_DETAIL_HV T where T."Node"='serverA%' and T."Timestamp"=
(select max(P."Timestamp") from DW.KPX_CPU_DETAIL_HV P where P."Node"='serverA%')

它似乎没有起作用,任何想法我可能在这里做错了吗?

2 个答案:

答案 0 :(得分:0)

试试这个,实际上可能比子选择更快(即使它是正确的):

SELECT T."Node",
       T."Timestamp",
       T."MAX_User_CPU_Pct", 
       T."MAX_System_CPU_Pct" 
FROM (
  SELECT p.*, 
         row_Number() over (partition by p."Node" order by p."Timestamp" desc) as rn
  FROM DW.KPX_CPU_DETAIL_HV p
) t
where rn = 1;

答案 1 :(得分:0)

SELECT T."Node",T."Timestamp",T."MAX_User_CPU_Pct", T."MAX_System_CPU_Pct" 
FROM (SELECT * FROM DW.KPX_CPU_DETAIL_HV T where T."Node" like 'serverA%' ORDER BY T."Timestamp" DESC) T
 WHERE ROWNUM = 1

这对我来说,不确定它是最好的解决方案,但现在正在努力。