求一SQL语句

xyqiqi 2006-05-17 11:39:19
原表字段
cardcode nowmoney
01 20
01 3.5
01 5
02 0
02 4
03 6
现想得到
cardcode mowmoney moneycount
01 20 20
01 3.5 23.5
01 5 28.5
02 0 0
02 4 4
03 6 6
...全文
99 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
FCU 2006-05-17
  • 打赏
  • 举报
回复
支持
lxzm1001 2006-05-17
  • 打赏
  • 举报
回复
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
lxzm1001 2006-05-17
  • 打赏
  • 举报
回复
没有ID真的是有点不好办
冷箫轻笛 2006-05-17
  • 打赏
  • 举报
回复
原表应该有ID一类的东西吧!?
paoluo 2006-05-17
  • 打赏
  • 举报
回复
如果順序不重要,且mowmoney沒有重復的話,可以不用臨時表,否則就要用到臨時表
Select
cardcode,
mowmoney,
(Select SUM(mowmoney) From TableName Where cardcode=A.cardcode And mowmoney<=A.mowmoney) As moneycount
From TableName A
昵称被占用了 2006-05-17
  • 打赏
  • 举报
回复
select cardcode,nowmoney,IDENTITY(int,1,1) as id
into #
from 原表

select cardcode,nowmoney,
(select sum(nowmoney) from # where cardcode=a.cardcode and id<=a.id) as moneycount
from # a

drop table #

34,592

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧