我的MSSQL2008工作中有一个问题,如下所述。我的疑问是:
;with WaitingCounts
AS
(
SELECT mt.id, COUNT(*) as cts
FROM #myTable mt
where mt.StatusId != 1
group by mt.id
)
UPDATE #myTable
SET mt.TotalWaitingRecords=WaitingCounts.cts
FROM #myTable mt
INNER JOIN WaitingCounts ON mt.id = WaitingCounts.id
我的问题是,当我只运行Select部分(AS(..)内部的那个)时,我看到了结果:
id cts
10 438
然后我通过以下方法删除statusId = 1的记录:
delete from #myTable where StatusId = 1
之后,当我运行整个代码,然后运行查询Select id, TotalWaitingRecords from #myTable where id=10 group by id, TotalWaitingRecords
时,我看到了:
id TotalWaitingRecords
10 802
我无法找出它发生的原因,802应该是438!如果有人给我一个线索,我很感激。感谢。