• MG-148046 (6/7/2010)


    I thought it would be an empty result but wasn't positive decided to "test" and created the table. I got this message on executing the query:

    Msg 8121, Level 16, State 1, Line 4

    Column 'QotD.col2' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

    This is from BOL:

    "The HAVING clause is typically used together with the GROUP BY clause to filter the results of aggregate values. However, HAVING can be specified without GROUP BY. The HAVING clause specifies additional filters that are applied after the WHERE clause filters. These filters can be applied to an aggregate function used in the select list."

    I don't know if the version of SQL makes a difference, but I am using SQL Server 2008 R2

    Hi MG,

    You didn't post the code you used, but I think that you used

    HAVING Col2 > 5

    whereas the QotD question uses

    HAVING MAX(Col2) > 5


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/