我在一张表中有以下数据......
Name CourseFee AdditionalCharges ExpenseAmount
Course-A 500 10 0
Course-B 250 0 0
Paid To Trainer 0 0 250
我想用sql查询跟随输出
Name CourseFee AdditionalCharges ExpenseAmount NetAmount
Course-A 500 10 0 510
Course-B 250 0 0 760
Paid To Trainer 0 0 250 510
NetAmount =(课程费用+附加费用 - 费用安装)
答案 0 :(得分:2)
您似乎想要字段的累积总和:
select t.*,
(select sum(t2.CourseFee + t2.AdditionalCharges - t2.ExpenseAmount)
from onetable t2
where t2.id <= t.id
) as NetAmount
from onetable t;
注意:
id
,但它可以是id,date或其他内容。 SQL表代表无序集,因此需要这样的列。outer apply
而不是相关子查询来执行此操作。