您必须进行自联接才能在 sql 查询中包含最新行和当前行...
Select c.InvNo, c.inStock current, r.InStock Previous
From table c join table r
on r.invNo = c.invNo
And r.AdjustedOn =
(Select Max(AdjustedOn)
From table
Where invNo = c.InvNo
And adjustedOn < c.AdjustedOn)
Where c.InStock >= r.Instock + 10 Or
c.Instock <= r.Instock - 10
这会为您提供在该时间点的更改与上一次更改相比为 10 或更多的所有行,但它包括在最近更改之前发生的过去更改。如果您希望查询仅包含表示最近更改的行,则将另一个谓词子查询添加到外部 sql...
Select c.InvNo, c.inStock current, r.InStock Previous
From table c join table r
on r.invNo = c.invNo
And r.AdjustedOn =
(Select Max(AdjustedOn)
From table
Where invNo = c.InvNo
And adjustedOn < c.AdjustedOn)
Where (c.InStock >= r.Instock + 10 Or
c.Instock <= r.Instock - 10)
And c.AdjustedOn =
(Select Max(AdjustedOn)
From table
Where invNo = c.InvNo)