SQL Server迭代求和

2021-09-07 23:17:15 字數 2924 閱讀 6385

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='geovindu' 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))

asbegin

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

endgo

select * from [dbo].[getvipnodlylist] ('geovindu') as a order by indate

---sql server聚合函式和子查詢迭代求和

---如果id不是第一條記錄,會出現第一行統計合計有問題,所以需查詢生成乙個新的id增長記錄

---銷售單號碼 銷售單日期 所得/已使用積分 可用積分 積分類別

select a.invoiceno as '銷售單號碼', a.indate as '銷售單日期',a.amount as '所得/已使用積分',

(select sum(amount) from [dbo].[getvipnodlylist] ('geovindu') b where b.vid <= a.vid) as '可用積分',a.dcr as '積分類別'

from [dbo].[getvipnodlylist] ('geovindu') a

SQL Server2000欄位累加求和

源表 num1 num2 date 100 300 2008 4 2 200 500 2008 4 3 300 800 2008 4 4 結果集 num1 num2 date 100 300 2008 4 2 300 800 2008 4 3 600 1600 2008 4 4 解決方法一 crea...

SQL Server聚合函式和子查詢迭代求和方法

2008 09 04 11 42 本文介紹了在sql server中使用聚合函式和子查詢迭代求和的公式與方法。先看看下面的表和其中的資料 t product 圖1該錶有兩個字段 xh和price,其中xh是主索引字段,現在要得到如下的查詢結果 圖2從上面的查詢結果可以看出,totalprice字段值...

迭代器 可迭代物件 迭代器物件

今天的學習內容有 迭代器 可迭代物件 迭代器物件 for迴圈內部原理 生成器和生成器表示式 面向過程程式設計 迭代就是指更新換代的過程,要重複進行,而且每次的迭代都必須基於上一次的結果。我們使用for迴圈的時候就是把元素從容器裡乙個個取出來,這種過程其實就是迭代。迭代器 迭代取值的工具。迭代器的作用...