访问-SQL 一阶差分

Access-SQL First Difference

构造给定日期的一阶差分 table 的最简单方法是什么。例如:

Date      | IDPosition | Quantity | Price | TotalPrice
14.04.16  | 1          | 1500     | 10    | 15000
15.04.16  | 1          | 2500     | 15    | 37500
----------------------------------------------------------
Diff      | 1          | 1000     | 5     | 22500

编辑:

知道了

SELECT (t1.Quantity - t2.Quantity) as QDiff
FROM table t1
INNER JOIN table t2
on t1.IDPosition = t2.IDPosition
where t2.Date = DateAdd('d', -1, t1.Date);

self join 可以用来做到这一点。它要求 table 具有 (PK) ID 或增量列。

select b.IDposition
      ,(b.quantity-a.quantity) as DiffQuantity
      ,(b.price-a.price) as DiffPrice
      , (b.totalprice-a.totalprice) as DiffTotalPrice 
from  table a 
inner join table b on a.ID=b.ID-1