Trouble with Query using MAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nico3334
    New Member
    • Aug 2007
    • 28

    Trouble with Query using MAX

    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!
  • azimmer
    Recognized Expert New Member
    • Jul 2007
    • 200

    #2
    Originally posted by nico3334
    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:
    Code:
    Select Date1
    FROM Table1
    WHERE Month(Date1) = [B](SELECT MAX(RIGHT(Date2, 2)) FROM Table1)[/B]

    Comment

    Working...