SQL UDF Group By Parameter Issue

Posted by Ryan Strauss on Stack Overflow See other posts from Stack Overflow or by Ryan Strauss
Published on 2010-04-26T21:51:44Z Indexed on 2010/04/26 21:53 UTC
Read the original article Hit count: 165

Filed under:

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.

© Stack Overflow or respective owner

Related posts about sql