SQL:存储列的计算值,并将其用于进一步操作

时间:2018-03-08 11:24:07

标签: sql sql-server aggregate-functions

我的表格包含IdGross AmountAmount Applied

ID | Gross Amt | Amt Applied | Outstanding Amount
1  |   100     |    10       |    90
1  |   100     |    10       |    80
1  |   100     |    10       |    70

如何为同一个ID的每列获得优秀的金额值。

由于

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以使用累计金额。但是,这取决于行的顺序。 SQL表表示无序集。您需要一列来指定排序。

假设您有这样一个列,您可以在大多数数据库中使用ANSI标准函数进行累积求和:

select t.*,
       (gross_amount - sum(amt_applied) over (partition by id order by ?)
       ) as outstanding_amount
from t;

?用于排序列。

相关问题