这个东东该怎么写?请指教!

moqiyayan 2003-08-15 04:43:58
我有一个表Table,有字段a(varchar),b(int)
eg:
a b
001 2
002 4
003 5
004 6
005 8
006 12

我想得到的这样一个表
a b c
001 2 2
002 4 6
003 5 11
004 6 17
005 8 25
006 12 37
即:c字段的值为前几条数据的b字段的合计值。
...全文
27 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
moqiyayan 2003-08-18
  • 打赏
  • 举报
回复
不好意思,还是不行啊。
如果不Insert的话,若a字段重复的得出的结果不正确阿
happy_0325 2003-08-18
  • 打赏
  • 举报
回复
你不需要insert的,@tb就是你的表名
select *, (select sum(b) from (select distinct * from @tb) as tmp1 where tmp1.a<=tmp.a) as c from (select distinct * from @tb) as tmp
moqiyayan 2003-08-18
  • 打赏
  • 举报
回复
我的表中有上千条数据,我不能一条一条的insert吧?!
还有,我可不可以把这个表生成一个视图?
txlicenhe 2003-08-18
  • 打赏
  • 举报
回复
sorry,如下:
Select identity(int,1,1) as id,* into #tmp from table
select a,b,(select sum(b) from #tmp where id <=tem.id) as c from #tmp tem
drop table #tmp

txlicenhe 2003-08-18
  • 打赏
  • 举报
回复
Select identity(int,1,1) as id,* into #tmp from table
select a,b,(select sum(b) from #tmp where a <=tem.a) as c from #tmp tem
drop table #tmp

yangvxin1 2003-08-18
  • 打赏
  • 举报
回复
给你用大力经典语句。呵呵.
p1(a,b)

select *,0 as i into #pp1 from p1

declare @p1 int

set @p1=0

update #pp1 set @p1=b+@p1,i=@p1

select * from #pp1

drop table #pp1
dafu71 2003-08-15
  • 打赏
  • 举报
回复
可是我的a字段不唯一的,可能会有重复的呢?

declare @tb table(a varchar(10),b int)
insert @tb values('001',2)
insert @tb values('001',2)
insert @tb values('002', 4)
insert @tb values('003', 5)
insert @tb values('004' , 6)
insert @tb values('005' , 8)
insert @tb values('006' , 12)
select *, (select sum(b) from (select distinct * from @tb) as tmp1 where tmp1.a<=tmp.a) as c from (select distinct * from @tb) as tmp

dafu71 2003-08-15
  • 打赏
  • 举报
回复
declare @tb table(a varchar(10),b int)
insert @tb values('001',2)
insert @tb values('002', 4)
insert @tb values('003', 5)
insert @tb values('004' , 6)
insert @tb values('005' , 8)
insert @tb values('006' , 12)

select *, (select sum(b) from @tb where a<=tmp.a) as c from @tb as tmp
moqiyayan 2003-08-15
  • 打赏
  • 举报
回复
可是我的a字段不唯一的,可能会有重复的呢?
pengdali 2003-08-15
  • 打赏
  • 举报
回复
select *,(select sum(b) from [table] where a <=tem.a) c from [table] tem
pengdali 2003-08-15
  • 打赏
  • 举报
回复
select *,(select sum(b) from [table] where a <=tem.a) c from [table] tem
moqiyayan 2003-08-15
  • 打赏
  • 举报
回复
谢谢!可以实现吗?请高手指点一二!

34,874

社区成员

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

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