我有一个存储过程,它遍历财政年度的几个月,并对每个月的项目进行计数。我知道事实上有 176 个项目,但是当我运行它时,它返回的总计数为 182。我尝试从 @EndDate 中删除一秒,但我的总计数为 165。所以我要么计算项目两次,要么不算全部。谁能帮助我在这里做错了什么?以下是我正在做的精简版:
DECLARE @Date DATETIME
DECLARE @EndDate DATETIME
SELECT @Date = CAST((@Year - 1) as VARCHAR) + '-07-01'
SELECT @EndDate = DATEADD(Month, 1, @Date)
DECLARE @Count INT
SELECT @Count = 0
WHILE @Count < 12
BEGIN
SELECT
COUNT(yai.ID)
FROM
table_1 yai
INNER JOIN table_2 yat ON yai.ID = yat.ID
WHERE
(yat.Date_Received BETWEEN CONVERT(VARCHAR, @Date, 101) AND CONVERT(VARCHAR, @EndDate, 101)) AND
yai.Pro_Type = @Value AND yat.Type = 'PC'
SELECT @Count = @Count + 1
SELECT @Date = DATEADD(MONTH, 1, @Date)
SELECT @EndDate = DATEADD(MONTH, 1, @EndDate)
END