Saturday, November 2, 2013

group by alias name?

Hi,


You can also try like this ,



Declare @ExportDate datetime='2013-10-23'
Declare @Code nchar(2)=47
Declare @AreaNumber nvarchar(4)=2301



select SUBSTRING(ExportEntityNumber,3,2) as DepartmentCode,COUNT(*)

from Rel_Entity_Indicator

where

ETC=565 and

SUBSTRING(ExportEntityNumber,len(ExportEntityNumber)-3,4)='2301' and

ExportDate>@ExportDate

group by SUBSTRING(ExportEntityNumber,3,2)

As said by Sarat , it is because of logical query processing order , you can use alias name of column in the SELECT statement for ORDER BY clause not for GROUP BY clause .


Related reference links :


http://blog.sqlauthority.com/tag/logical-query-processing-phases/


http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/




sathya - www.allaboutmssql.com ** Mark as answered if my post solved your problem and Vote as helpful if my post was useful **.


No comments:

Post a Comment