【问题标题】:Error in using aggregate function max() oracle使用聚合函数 max() oracle 时出错
【发布时间】:2013-03-14 21:48:33
【问题描述】:

我正在尝试运行查询

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

我做错了什么?

谢谢

【问题讨论】:

    标签: oracle11g


    【解决方案1】:

    您不能在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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 2021-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多