1

I am trying to run query

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

But i am getting error that

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

what i am doing wrong?

Thanks

2 Answers 2

3

You can't use an aggregate in a where clause. Also, you can't mix non-aggregated data and aggregated data from the same column in a single select. You'll need to use a sub-query:

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);
Sign up to request clarification or add additional context in comments.

Comments

2

you cannot reference an aggregate like that. you would either have to put a sub query up like below (assuming you wanted the max(finish_date) to mean the max finish date for step 011 and not the max finish date in the whole table (which may return no rows):

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

or use an analytic function

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.