How to Use Aggregate (MAX) in Where Clause?

ilcaa

Well-known Member
Joined
May 25, 2005
Messages
751
Office Version
  1. 365
Platform
  1. Windows
I have a Table, "Quotes", that is structured

qDate, qTicker, qClose
4/20/2015, goog, 540.93
4/19/2015, goog, 539.30
4/18/2015, goog, 537.10
....
3/19/2014, goog, 539.30
3/18/2014, goog, 537.10

I am trying to get the qClose of the last available Date (qDate). I am using the MAX(qDate) to find the last date but if I use it in my WHERE clause it errors, "Aggregate Functions not available in WHERE clause"

Code:
Select Quotes.qClose
From Quotes
Where ((qTicker = 'goog') AND (qDate = Max(qDate));
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can write a subquery instead or join the query to another aggregate query of all the groups' max values.
 
Upvote 0
im not that versed in Queries

the subquery, getting the Max(aDate), by itself returns the correct date of 4/20/2015 but the entire query doesnt return the qClose of that Date

Code:
Select Quotes.qClose
From Quotes
Where ((qTicker = 'goog') AND 
(Select Max(qDate)
from Quotes
WHERE qTicker ='goog');
 
Upvote 0
thanks for the advise, I finnally got it to work...

Code:
Select qCLose
From Quotes
Where qTicker ='goog' and qDate=
(select Max(qDate) from Quotes WHERE qticker='goog');
 
Upvote 0

Forum statistics

Threads
1,221,875
Messages
6,162,563
Members
451,775
Latest member
Aiden Jenner

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top