请教SQL问题
用SQL实现Bn=Bn-1+An(n>=2)
如表T1:
seq a b
1 0 100
2 -200 0
3 100 0
4 200 0
5 300 0
6 -400 0
……
使用上面公式计算出B
结果
seq a b
1 0 100
2 -200 -100
3 100 0
4 200 200
5 300 500
6 -400 100
……
select seq , a,b=a - (select sum(b) from t1 where seq < t.seq) from t1 t
SQL code:
use tempdb
if object_id('tb') is not null drop table tb
go
create table tb([seq] INT,[a] INT,[b] INT)
insert into tb
select 1,0,100 union all
select 2,-200,0 union all
select 3,100,0 union all
select 4,200,0 union all
select 5,300,0 union all
select 6,-400,0
go
--select * from tb
update tb set b = (select sum(t.a+t.b) from tb t where t.seq<=tb.seq)
select * from tb
/*
seq a b
----------- ----------- -----------
1 0 100
2 -200 -100
3 100 0
4 200
相关问答:
从数据库中查询一张表的数据
select 部门,姓名 from tb
如何才能生成下面的xml格式
XML code:
<folder state="unchecked" label="全部">
<folder state="unchecked&qu ......
我是在toad中输入下段sql
declare
TYPE test_rec IS record(
code varchar(10),
name varchar(30)
);
v_book test_rec;
......
sql的软件在哪里可以下啊!在网上找了蛮多都用不了啊
随便搞一D版吧,
迅雷第一个就可以用
2000,2005都这样
http://119.147.41.16/down?cid=0698C2D64D7D637D90A6D2482298E6717D4F15CD&t=2&fmt=-1 ......
第一句:
select ht.gfdm,gfdm.gfmc,gfdm.lb ,sum(jh.htdj*jh.htsl)as je
from ht left join gfdm on ht.gfdm=gfdm.gfdm
join jh on ht.htbh=jh.hth
and ht.htbh not like'del%' and ht. ......