有更简单的方法: no val 1 19 2 37 3 43 4 98到表二:no val tmp 1 19 19 2 37 19 3 43 37 4 98 43一个循环中用一个变量就可得到tmp列的值,然后用update .... set val=val-tmp
select *,val=val-isnull((select max(val) from test where a.no>no),0) from test a注意 : test换成表名
再修改一下: select *,val=val-isnull((select val from test where a.no>no),0) from test a order by no 注意 : test换成表名
上面的在SQL SERVER中可以测试
have a try select a.no, (b.val-a.val) as val from your_table a, your_table b where b.no = a.no + 1
select t1.no, t1.var-isnull(t2.var,t1.var) from t t1 left join t t2 on t1.no=t2.no+1
以上的都通不过。我把问题描述详细一点, 在sql server中,就只有一张表 ,table1 内容 no val 1 19 2 37 3 43 4 98 用sql语句,能显示出 no val val2 1 19 0 2 37 18 3 43 6 4 98 55 的效果,没有第二张表,也不用修改原table1的值。
写得比较粗糙,太晚了,困。--创建函数得到val2 CREATE FUNCTION [dbo].[Fun_Test] ( @intNo INT )RETURNS INT AS BEGIN declare @rtValue INT select @rtValue = +(max(val)-min(val)) from ( Select top 2 val From table1 Where no1<=@intNo order by no1 desc) aReturn (ISNULL(@rtValue,0))ENDgo --建测试表 Create Table table1(no1 int,val int)go --插入测试记录 Insert into table1 values (1,19) Insert into table1 values (2,37) Insert into table1 values (3,43) Insert into table1 values (4,98)go --查询 select no1,val,dbo.Fun_Test(no1) from table1go --删除函数和测试表 Drop FUNCTION [dbo].[Fun_Test] Drop Table table1
sqlserver可以这样写: select a.nol,a.val,a.val-isnull(b.val,a.val) from test_01 a,test_01 b where a.nol*=b.nol+1 得到的结果就是 nol val (No column name) 1 19 0 2 37 18 3 43 6 4 98 55
--建立测试环境 Create Table 表(no integer,val integer) --插入数据 insert into 表 select '1','19' union select '2','37' union select '3','43' union select '4','98' select * from 表 --测试语句 select top 1 no,0 from 表 union select b.no,b.val-a.val from 表 a,表 b where a.no=b.no-1
然后重首记录开始搜索,一直到末位记录。然后把第3个变量付值给表2中的val项,就搞定了
no val
1 19
2 37
3 43
4 98到表二:no val tmp
1 19 19
2 37 19
3 43 37
4 98 43一个循环中用一个变量就可得到tmp列的值,然后用update .... set val=val-tmp
select *,val=val-isnull((select val from test where a.no>no),0) from test a
order by no 注意 : test换成表名
select a.no, (b.val-a.val) as val
from your_table a,
your_table b
where b.no = a.no + 1
from t t1 left join t t2 on t1.no=t2.no+1
在sql server中,就只有一张表 ,table1
内容
no val
1 19
2 37
3 43
4 98
用sql语句,能显示出
no val val2
1 19 0
2 37 18
3 43 6
4 98 55
的效果,没有第二张表,也不用修改原table1的值。
CREATE FUNCTION [dbo].[Fun_Test]
(
@intNo INT
)RETURNS INT
AS
BEGIN
declare @rtValue INT
select @rtValue = +(max(val)-min(val)) from (
Select top 2 val From table1 Where no1<=@intNo
order by no1 desc) aReturn (ISNULL(@rtValue,0))ENDgo
--建测试表
Create Table table1(no1 int,val int)go
--插入测试记录
Insert into table1 values (1,19)
Insert into table1 values (2,37)
Insert into table1 values (3,43)
Insert into table1 values (4,98)go
--查询
select no1,val,dbo.Fun_Test(no1) from table1go
--删除函数和测试表
Drop FUNCTION [dbo].[Fun_Test]
Drop Table table1
no1 val 无列名
————————————
1 19 0
2 37 18
3 43 6
4 98 55
————————————
select a.nol,a.val,a.val-isnull(b.val,a.val)
from test_01 a,test_01 b
where a.nol*=b.nol+1
得到的结果就是
nol val (No column name)
1 19 0
2 37 18
3 43 6
4 98 55
Create Table 表(no integer,val integer)
--插入数据
insert into 表
select '1','19' union
select '2','37' union
select '3','43' union
select '4','98'
select * from 表
--测试语句
select top 1 no,0 from 表
union
select b.no,b.val-a.val from 表 a,表 b
where a.no=b.no-1
--删除测试环境
Drop Table 表