Msg 147, Level 15, State 1, Line 1
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.
This error message appears when you try to use an aggregate function in a WHERE clause in an invalid context.
The T-SQL statement can be parsed, but causes the error at runtime.
Errors of the Severity Level 15 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. You must modify the WHERE clause.
All versions of SQL Server.
WHERE MAX(OrderDate) < GETDATE()
In the above example we try to the MAX aggregate function in an invalid context in the WHERE clause. This raises the error.