I have this query which works fine with SQL Server 2008:
SELECT COALESCE(LastName + ', ' + FirstName, LastName, FirstName) [CalculatedEmployeeName]
FROM Emp_General
ORDER BY [CalculatedEmployeeName] ASC
(Notice that it is ordering by the CalculatedEmployeeName field and not complaining)
When I add a Row_Number field as follows:
SELECT COALESCE(LastName + ', ' + FirstName, LastName, FirstName) [CalculatedEmployeeName]
, Row_Number() Over (Order BY [CalculatedEmployeeName] ASC) RecordNumber
FROM Emp_General
ORDER BY [CalculatedEmployeeName] ASC
I get an error of:
Invalid column name 'CalculatedEmployeeName'.
Any ideas on why it is complaining?
Note that I have already tried using
row_number() OVER (ORDER BY (SELECT 0))
and
row_number() over (order by @@rowcount)
as discussed in:
SQL Row_Number() function in Where Clause without ORDER BY?
But they do not return the row_number in the correct order.