开发者

SQL MAX date in where clause

开发者 https://www.devze.com 2023-01-25 14:15 出处:网络
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:

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

精彩评论

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

关注公众号