By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,301 Members | 3,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,301 IT Pros & Developers. It's quick & easy.

Trouble with Query using MAX

P: 28
I'm having trouble with a query using "MAX". In my query I'm trying to use 2 date columns as my criteria. Here is the format for each column:

Date1:
01/01/2007

Date2:
200701

For my query, I'm trying to pull the Date1 values where the month of Date1 equals the Max month of Date2. Here is my coding:

Select Date1
FROM Table1
WHERE Month(Date1) = MAX(RIGHT(Date2, 2))

But I'm getting the error: "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference".

I'm not exactly sure what I'm doing wrong. If someone could help me with some examples, I'd greatly appreciate it.

Thanks!
Sep 21 '07 #1
Share this Question
Share on Google+
1 Reply


azimmer
Expert 100+
P: 200
I'm having trouble with a query using "MAX". In my query I'm trying to use 2 date columns as my criteria. Here is the format for each column:

Date1:
01/01/2007

Date2:
200701

For my query, I'm trying to pull the Date1 values where the month of Date1 equals the Max month of Date2. Here is my coding:

Select Date1
FROM Table1
WHERE Month(Date1) = MAX(RIGHT(Date2, 2))

But I'm getting the error: "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference".

I'm not exactly sure what I'm doing wrong. If someone could help me with some examples, I'd greatly appreciate it.

Thanks!
Select the Max date with a separate SELECT like this:
Expand|Select|Wrap|Line Numbers
  1. Select Date1
  2. FROM Table1
  3. WHERE Month(Date1) = (SELECT MAX(RIGHT(Date2, 2)) FROM Table1)
  4.  
Sep 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.