我有一个简化的数据库,如下所示:
ID | SecondID | DateRecorded | NumberToCheck | NumberToUpdate
NumberToUpdate
当前所有行的值为 0。
我希望用同一行上NumberToUpdate
的值进行更新,减去 具有最早“ ”的值,其中大于 0,并且具有与原始行相同的 ID 和 secondID。NumberToCheck
NumberToCheck
DateRecorded
(Min(DateRecorded)
NumberToCheck
到目前为止我有
UPDATE dbo.Table
SET NumberToUpdate =//Update NumberToUpdate
NumberToCheck - //NumberToCheck from the current row, subtracting...
(SELECT TOP 1 t2.NumberToCheck FROM Table t2 WHERE ID = t2.ID AND secondID = t2.secondID AND t2.DateRecorded =
(SELECT TOP 1 MIN(t3.DateRecorded) FROM Table t3 WHERE t2.ID = t3.ID AND t2.secondID = t3.secondID AND t3.Passes > 0))
//the NumberToCheck with the earliest date, of the same ID.
然而,这是不正确的,并且返回给我没有意义的值(包括不应该有的负值!)
我在这里忘记了什么?
非常感谢