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.

Error Message:
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
.

Severity level:
15.

Description:
This error message appears when you try to use an aggregate function in a WHERE clause in an invalid context.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
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.

Versions:
All versions of SQL Server.

Example(s):
SELECT *
  FROM Northwind.dbo.Orders
 WHERE MAX(OrderDate) < GETDATE()

Remarks:
In the above example we try to the MAX aggregate function in an invalid context in the WHERE clause. This raises the error.

]]>

Leave a comment

Your email address will not be published.