为给定日期构建第一个差异表的最简单方法是什么。 E.g:
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);
答案 0 :(得分:0)
自联接可以用来做到这一点。它要求表具有(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