两个查询返回不同的数字

Aar*_*ron 1 sql sql-server

为什么这些查询给出不同的结果

查询1

SELECT DATEPART(YEAR, Cancel) [Year],
DATEPART(Month, Cancel) [Month], COUNT(1) [Count]
FROM Subscription
where DATEPART(YEAR, Cancel) = 2016
GROUP BY DATEPART(year, Cancel),DATEPART(Month, Cancel)
Run Code Online (Sandbox Code Playgroud)

该查询为我提供了2016年每个月的取消信息。

查询2

这使我在2016年的第9个月被取消。

select count(*) from Subscription
where Cancel >= '2016-09-01 00:00:00.000'
and Cancel <= '2016-09-30 00:00:00.000'
Run Code Online (Sandbox Code Playgroud)

这些数字相差5万。Query 1返回的会员人数比Query 2

The*_*ler 5

简单。在第二个查询中,您错过了2016年9月30日一个月的最后一天的所有取消。

您应该使用: and Cancel < '2016-10-01 00:00:00.000'