HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

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

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlbeingrecognizedservercountnotalias

Problem

I have restored the Northwind database(https://northwinddatabase.codeplex.com/) from a backup file using SQL Server.

Trying to execute the following query results in error "Invalid column name 'products'.", product is an alias.

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


What is the problem?

Solution

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.



  • FROM



  • ON



  • JOIN



  • WHERE



  • GROUP BY



  • WITH CUBE or WITH ROLLUP



  • HAVING



  • SELECT



  • DISTINCT



  • ORDER BY



  • 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;

Code Snippets

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

Context

StackExchange Database Administrators Q#126996, answer score: 23

Revisions (0)

No revisions yet.