• dbowlin (6/7/2010)


    I am not sure I understand the logic behind this one.

    This query will result in a single unnamed column with a value of 5

    SELECT COUNT(*)

    FROM QotD

    WHERE col2 <> 4

    How does adding the

    HAVING MAX(col2) > 5

    end up with the same result?

    Thanks,

    Hi dbowlin,

    The reply posted by wware might give you a clue.

    The first form is quite well known. We all use SELECT COUNT(*) FROM SomeTable, or SELECT MAX(SomeColumn) FROM SomeTable occasionaly, and expect SQL Server to calculate the aggregate over the entire table, returning a single row.

    The HAVING builds on that. There still is a single group containing all rows (except those filtered by the WHERE). If that group satisfies the HAVING, you get a result. If it doesn't the group is discarded and since there are no other groups, the result is empty.


    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/