I have a problem trying to make an alias for new column and using it in GROUP BY clause:
SELECT TOP 100 Percent
count(id) AS [items_by_day],
(SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, [date]))) AS [date_part]
FROM [MyDB].[dbo].[MyTable]
GROUP BY DAY([date]), MONTH([date]), YEAR([date]), date_part
I get the following error:
Msg 207, Level 16, State 1, Line 5
Invalid column name 'date_part'.
How is it possible to solve the problem?
How about a subquery?
See my demo at sqlfiddle
The part
(SELECT DateAdd(... DateDiff(...))seems to return the plain date. Can you explain what i am missing?