create table a(cardcode varchar(10),nowmoney money)
insert a select '01',20
union all select '01',3.5
union all select '01',5
union all select '02',0
union all select '02',4
union all select '03',6
select id=identity(int),* into b from a
select *,moneycount=(select sum(nowmoney) from b where cardcode=t.cardcode and id<=t.id) from b t
如果順序不重要,且mowmoney沒有重復的話,可以不用臨時表,否則就要用到臨時表
Select
cardcode,
mowmoney,
(Select SUM(mowmoney) From TableName Where cardcode=A.cardcode And mowmoney<=A.mowmoney) As moneycount
From TableName A