2013-03-14 50 views
1

我試圖運行查詢錯誤使用聚合函數MAX()甲骨文

select * from OS_Historystep where step_name = '011' and finish_date = max(finish_date) ; 

但是我得到錯誤

ORA-00934: group function is not allowed here 
00934. 00000 - "group function is not allowed here" 
*Cause:  
*Action: 
Error at Line: 12 Column: 72 

什麼,我做錯了什麼?

謝謝

回答

3

你不能在where子句中使用的總和。此外,你不能在一個單一的選擇混合來自同一列非總量數據和彙總數據。您將需要使用子查詢:

select * 
from OS_Historystep hs1 
where step_name = '011' 
and finish_date = (select max(finish_date) 
        from OS_Historystep hs2 
        where hs2.step_name = hs1.step_name); 
2

你不能引用這樣的聚合。你要麼必須把一個子查詢像以下(假設你想要的max(finish_date)意味着對步011的最大結束日期,而不是最大結束日期在整個表(可能不返回行):

select * 
    from OS_Historystep 
where step_name = '011' 
    and finish_date = (select max(finish_date) 
         from OS_Historystep 
         where step_name = '011'); 

或使用的解析函數

select * 
    from (select s.*, rank() over (partition by step_name order by finish_date desc) rnk 
      from OS_Historystep s 
     where step_name = '011') 
where rnk = 1;