Skip to content
Advertisement

Get the newest record using date and time portion, getting error on aggregate function

I am trying to get the most recent record(s) with a status of Registered, Expired, using the status date value to get the latest one.

The query I have , I am using a specific lot number that has 13 record ids to it.

I get this list:

You can see that the most recent is the one at the top LR2014-00519, basing that on the value of STATUS_DATE time portion.

I then added another MAX around the RECORD_ID column, and an ORDER BY STATUS_DATE

so the revised query now looks like:

But when I run it I get an error that reads:

But I have the STATUS_DATE in an aggregate function MAX within the SELECT.

What am I doing wrong? I hope someone can offer some help to correct this.

Thank you kindly.

Advertisement

Answer

But I have the STATUS_DATE in an aggregate function MAX within the SELECT.

This due to the aggregation, as usual the STATUS_DATE disappears upon aggregation, in your case it wouldn’t do the order by unless you specify alias MAX(STATUS_DATE) AS STATUS_DATE

However, I believe, ROW_NUMBER() and Sub query would help you in your case, try following:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement