Alias of COUNT is not being "recognized" by SQL Server

The SELECT clause is logically processed after the HAVING clause. Therefore the aliases used in SELECT don't exist (yet) when the HAVING clause is processed.

On MSDN you can look at SELECT (Transact-SQL):

Logical Processing Order of the SELECT statement

The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. WITH CUBE or WITH ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY
  11. TOP

This query works because it repeats what will later be defined (the COUNT) in the SELECT clause:

SELECT OrderID, COUNT(ProductID) products
FROM [NORTHWND].[dbo].[OrderDetails]
GROUP BY OrderID
HAVING COUNT(ProductID) > 5;

Because the HAVING is parsed before the alias has been generated in the SELECT list. This is explained in much detail here.

The only place you could use an alias is in the ORDER BY clause. A workaround is to repeat the expression in the HAVING:

    SELECT OrderID, COUNT(ProductID) AS Products
      FROM Northwind.dbo.OrderDetails
      GROUP BY OrderID
      HAVING COUNT(ProductID) > 5;

Tags:

Sql Server