请教一个SQL语句
有一个表TB,其结构和数据如下
ID VALUE
A 10
B 20
C 30
D 40
通过一查询后得到如下数据
ID VALUE
A 10
B 30
C 60
D 100
请各位大哥帮帮忙
问题点数:20、回复次数:6Top
1 楼lsxaa(小李铅笔刀)回复于 2004-11-04 10:59:17 得分 10
select id,(select sum(value) from tb where value<=a.value) as value
from tb aTop
2 楼davorsuker39(大狐狸)回复于 2004-11-04 11:30:30 得分 0
UP lsxaa(小李铅笔刀)Top
3 楼shuiniu(飞扬的梦)(我是一头只吃西红柿的水牛)回复于 2004-11-04 11:51:31 得分 0
--or
select a.id,sum(b.value) from test a join test b on a.id >= b.id
group by a.id,a.valueTop
4 楼yanzheng1(光原)回复于 2004-11-04 12:58:22 得分 10
楼上的都有问题,如果VALUE不是按顺序排列的话就出错?
declare errors scroll cursor for
select id,value from tb
open errors
if exists (select name from sysobjects where name = 'table_rate_yzg_p3') drop table table_rate_yzg_p3
create table table_rate_yzg_p3
(
id varchar(10),
value int,
)
go
declare @i int,@row int,@id char(10),@value int,@zong int
set @i=0
set @zong=0
label:
set @i=@i+1
fetch next from errors into @id,@value
begin
set @zong = @zong + @value
insert into table_rate_yzg_p3 select @id,@zong
end
if @i<@@cursor_rows
goto label
select * from table_rate_yzg_p3
if exists (select name from sysobjects where name = 'table_rate_yzg_p3') drop table table_rate_yzg_p3
close errors
deallocate errorsTop
5 楼lsxaa(小李铅笔刀)回复于 2004-11-04 13:00:13 得分 0
ID 总该不重复吧
select id,(select sum(value) from tb where ID<=a.ID) as value
from tb a
order by ID
Top
6 楼lzccaxwx(天涯寻觅)回复于 2004-11-04 14:09:12 得分 0
ID不会重复,谢谢各位大哥的帮忙Top




