查询运行减法

时间:2015-03-01 15:08:33

标签: sql ms-access

我正在处理一个项目,我需要计算所有到期日 付款。我在Query中发了一个查询 设计。但我无法通过查询获得全额付款金额  将从之前的DueAmount值中减去amount和DueAmount列值。这是查询:

SELECT Payment.PayDate, OrderMaster.OrderId, OrderMaster.CustomerName, OrderMaster.TotalAmount, Payment.PayAmount, OrderMaster.TotalAmount - Payment.PayAmount AS DueAmount FROM OrderMaster INNER JOIN Payment ON OrderMaster.OrderId = Payment.OrderId;

我收到的查询结果如下:

   PayDate   | OrderId  | CustomerName | TotalAmount | PayAmount | DueAmount
   -------------------------------------------------------------------------
   20/12/2014 |     1   | Someone      |   5000      |  2000      | 3000
   22/12/2014 |     1   | Someone      |   5000      |  1000      | 4000
   25/12/2014 |     1   | Someone      |   5000      |  2000      | 3000

我希望查询结果如下:

    PayDate   | OrderId | CustomerName | TotalAmount | PayAmount | DueAmount
   -------------------------------------------------------------------------
   20/12/2014 |     1   | Someone      |   5000      |  2000      | 3000
   22/12/2014 |     1   | Someone      |   5000      |  1000      | 2000
   25/12/2014 |     1   | Someone      |   5000      |  2000      | 0

请帮我准确查询以获得此结果。感谢。

1 个答案:

答案 0 :(得分:0)

You may try like this:
select myField, myField -
(select myField from myTable where myOrderField =
(select max(myOrderField) from myTable where myOrderField < T.myOrderField))
as RunningDiff
from myTable as T