drop table t_geovindu
create table t_geovindu
(
xid int IDENTITY (1, 1),
price money,
DebitCredit VARCHAR(2),
adate datetime default(getdate())
)
insert into t_geovindu(DebitCredit,price) values('C',10)
insert into t_geovindu(DebitCredit,price) values('C',25)
insert into t_geovindu(DebitCredit,price) values('C',36)
insert into t_geovindu(DebitCredit,price) values('C',66)
insert into t_geovindu(DebitCredit,price) values('D',-11)
insert into t_geovindu(DebitCredit,price) values('C',32)
insert into t_geovindu(DebitCredit,price) values('D',-50)
--
select a.xid, a.price,
(select sum(price) from t_geovindu b where b.xid <= a.xid) as Balance,DebitCredit
from t_geovindu a
--
select xid, price,
(case when Balance is null then price else Balance end ) as Balance
from
(select a.xid, (select sum(price) f
SQL Server迭代求和
最新推荐文章于 2023-06-29 04:37:34 发布
这篇博客介绍了如何在SQL Server中进行迭代求和,包括创建数据表、插入记录,然后通过子查询和聚合函数计算每个记录的累计余额。还展示了如何创建用户定义函数以获取指定VIP客户的交易累计总额,并在数据表中使用该函数。文章最后讨论了在处理非第一条记录时可能出现的问题及解决方法。
摘要由CSDN通过智能技术生成