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) from t_geovindu b where b.xid < a.xid) as Balance , a.price
from t_geovindu a) x
--
select sum(price) from t_geovindu b where (b.xid < a.xid)
select a.xid, (select sum(price) from t_geovindu b where b.xid < a.xid) as Balance , a.price
from t_geovindu a
---
create function mysum(@xh int, @price int) returns int
begin
return (select
(case when Balance is null then @price else Balance end) as Balance
from ( select sum(price) as Balance from t_geovindu where xid < @xh) x)
end
---
select xid, price, dbo.mysum(xid, price) as Balance
from t_geovindu
create table vipnoDly
(
VID Int IDENTITY (1, 1) PRIMARY KEY, invoiceno nvarchar(50),indate datetime, vipno nvarchar(50),amount int,dcr nvarchar(20)
)
go
SET IDENTITY_INSERT [dbo].vipnoDly ON
Insert vipnoDly(invoiceno,indate,vipno,amount,dcr) Select invoiceno,indate,vipno,amount,dcr From vipdly AS A Where vipno='654321' order by A.indate
select * from vipnoDly
--SET IDENTITY_INSERT dbo.Tool ON
Create Function [dbo].[GetVipNoDlyList]
(
@ID nvarchar(20)
)
Returns @Tree Table (VID Int IDENTITY (1, 1), invoiceno nvarchar(50),indate datetime, vipno nvarchar(50),amount int,dcr nvarchar(20))
As
Begin
Insert @Tree(invoiceno,indate,vipno,amount,dcr) Select invoiceno,indate,vipno,amount,dcr From vipdly AS A Where vipno=@ID order by A.indate
Return
End
GO
select * from [dbo].[GetVipNoDlyList] ('geovindu') as a order by indate
---SQL Server聚合函数和子查询迭代求和
---如果ID不是第一條記錄,會出現第一行統計合計有問題,所以需查詢生成一個新的ID增長記錄
select a.VID, a.amount,
(select sum(amount) from [dbo].[GetVipNoDlyList] ('geovindu') b where b.VID <= a.VID) as Balance
from [dbo].[GetVipNoDlyList] ('geovindu') a
SQL Server迭代求和
原创
©著作权归作者所有:来自51CTO博客作者geovin的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SQL Server 账号管理1
SQL Server 账号管理主要包含登录名、用户、架构、角色等管理。通过对账号的管理可以有效的提高数据库系统的安全性,规范运维及使用。注意:登录名是实例下的安全对象,用户、架构及角色是数据库下的安全对象
登录名 用户名 数据库级的主体 SQL Server 级的主体 安全对象 -
opencv imraed 头文件
OpenCV包含的模块 cv – 核心函数库 cvaux – 辅助函数库 cxcore – 数据结构与线性代数库 highgui – GUI函数库 ml – 机器学习函数库 函数名: Mod 矩阵数据类型: E
opencv imraed 头文件 #include 整型 函数库