我遇到了问题。我只想计算我的桌子的余额。
我的选择陈述是:
select date, ammount, ?? as Balance
from table
where accountnr = 123
order by date
输出应如下所示:
Date Ammount Balance
07/02/2016 -145.55 945.65 25/01/2016 349.45 1091.20 11/11/2015 340.25 741.75 30/09/2015 369.10 401.50 05/04/2015 32.40 32.40
我试了这么久,用不同的方式没有运气。
答案 0 :(得分:2)
您可以使用分析查询在单个表扫描(即没有任何连接或相关子查询)中执行此操作:
SELECT "date",
amount,
SUM( amount ) OVER ( ORDER BY "date" ) AS balance
FROM your_table;
如果表格中有多个帐户:
SELECT account_number,
"date",
amount,
SUM( amount ) OVER ( PARTITION BY account_number ORDER BY "date" ) AS balance
FROM your_table;