开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜