SQL UDF Group By Parameter Issue
- by Ryan Strauss
I'm having some issues with a group by clause in SQL. I have the following basic function:
CREATE FUNCTION dbo.fn_GetWinsYear (@Year int)
RETURNS int
AS
BEGIN
declare @W int
select @W = count(1)
from tblGames
where WinLossForfeit = 'W' and datepart(yyyy,Date) = @Year
return @W
END
I'm trying to run the following basic query:
select dbo.fn_GetWinsYear(datepart(yyyy,date))
from tblGames
group by datepart(yyyy,date)
However, I'm encountering the following error message: Column 'tblGames.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Any ideas why this is occurring? FYI, I know I can remove the function and combine into one call but I'd like to keep the function in place if possible.