SQL MAX date in where clause
I need to use the MAX function in my where clause because it is giving me the wrong data if I do not. Here 开发者_StackOverflowis my code:
(SELECT
Index,
Company,
FormType,
MAX(DocumentDate) AS DocumentDate
FROM dbo.FormInstance AS F
WHERE f.company = 1234
AND (
CAST(FLOOR(CAST([DocumentDate] AS FLOAT))AS DATETIME)
BETWEEN CAST(FLOOR(CAST(@StartDate AS FLOAT))AS DATETIME)
AND CAST(FLOOR(CAST(@EndDate AS FLOAT))AS DATETIME)
)
GROUP BY
Company,
Index,
FormType);
How would I make is say AND MAX(DocumentDate) is BETWEEN @Start and @End?
Thanks
Add HAVING MAX(DocumentDate) BETWEEN @Start AND @End
after your GROUP BY statement.
HAVING is essentially the WHERE clause of aggregation.
精彩评论