• Others have already pointed out the missing GROUP BY clause, so I'll skip that and limit myself to commenting the non-portable date format used. On many non-US systems, the date '1/31/09' will cause an error message to pop up. The correct format would be: BETWEEN '20090101' AND '20090131'

    And if the version is SQL2005 or less, or if it's SQL2008 but the data type is not "date" but one of the formats that include a time component as well, then it should even be

    WHERE salesdate >= '20090101'

    AND salesdate < '20090201'

    because otherwise all sales made after Jan 31st, 0:00 will be excluded.


    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/