SQL Server迭代求和

这篇博客介绍了如何在SQL Server中进行迭代求和,包括创建数据表、插入记录,然后通过子查询和聚合函数计算每个记录的累计余额。还展示了如何创建用户定义函数以获取指定VIP客户的交易累计总额,并在数据表中使用该函数。文章最后讨论了在处理非第一条记录时可能出现的问题及解决方法。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值