Thursday, March 15, 2007

A Little Question...!

In my last post, I have explained how CTE can be used in SQL Server 2005. Just take one query used in that post leads to the another topic.

Select EmployeeId,Year(OrderDate) AS OrderYear,
Count(*) as [Sales]
From dbo.Orders
Where EmployeeId=1
Group by EmployeeId,Year(OrderDate)
Order By OrderYear


In the above query the
column Year(OrderDate) is having a alias OrderYear. But this alias is not used in the Group By Clause why? Again the alias is used in Order By Clause. Why don't we use the column alias in the select list can not be used in Group By as in Order By?

Post your comments...!

3 comments:

Unknown said...

Very excellent question... i learnt a new thing in sql today... thanx...keep on posting...

Anonymous said...

gr8 Man,

Kumar S said...

Are you not writing nowadays?

No articles/posts?