1

我正在尝试运行查询

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

我做错了什么?

谢谢

4

2 回答 2

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);
于 2013-03-14T13:48:33.523 回答
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;
于 2013-03-14T13:47:53.333 回答