I must be missing something obvious but why does the following
SELECT c.ContractID, max( cs.ContractStatusCreated)
FROM dbo.NMPT_Contract AS c INNER JOIN
dbo.NMPT_ContractStatus AS cs ON c.ContractID = cs.ContractID INNER JOIN
dbo.CMSS_Status AS s ON cs.StatusID = s.StatusID
group by c.ContractID
having cs.ContractStatusCreated = MAX(cs.ContractStatusCreated)
return the following from SQL Server 2000?
Msg 8121, Level 16, State 1, Line 1 Column ‘cs.ContractStatusCreated’
is invalid in the HAVING clause because it is not contained in either
an aggregate function or the GROUP BY clause.
Isn’t MAX an aggregate function?
Because
cs.ContractStatusCreatedis not in the group-by clause, therefore it does not know where to get the data from.And better yet, if you were to group by it you would end up with a
HAVING 1=1condition and yourmax(cs.ContractStatusCreated)wouldn’t do what you want. I think you need a self-join or subquery to find your max value to compare to.