MSSQLServer求相邻两行时间差的问题
作者:小骇 日期:2009-05-10
表概况:
.....交易时间...........当前余额.....
.....(1)xxxx-xx-xx ........40000.0.....
.....(2)xxxx-xx-xx.........30000.0.....
.....(3)xxxx-xx-xx.........20000.0.....
.....(4)xxxx-xx-xx.........10000.0.....
大概是这种情况,我想求相邻两行的 交易时间 的差,注意是相邻两行,然后用40000.0×时间(2)与时间(1)的差×日利率=利息。
解决方法:
--id为(1),(2)列行字段
select datadiff(dd,a.交易时间,b.交易时间)
from tableName a
left join tableName b on a.id = b.id+1
存储过程:
declare @t table(id int,d datetime,v float)
insert into @t select 1,'2008-01-05',4000
insert into @t select 2,'2008-01-03',2000
insert into @t select 3,'2008-01-02',1000
declare @利率 float
set @利率=0.03
select a.*,a.v*datediff(d,b.d,a.d)*@利率 from @t a left join @t b on a.id=b.id-1
/*
id d v
----------- ----------------------- ---------------------- ----------------------
1 2008-01-05 00:00:00.000 4000 240
2 2008-01-03 00:00:00.000 2000 60
3 2008-01-02 00:00:00.000 1000 NULL
*/
或
create table tbf(id int,tim varchar(10),acc decimal(22,4))
insert into tbf values(1,'2009/01/01',40000)
insert into tbf values(2,'2009/02/01',30000)
insert into tbf values(3,'2009/03/01',20000)
insert into tbf values(4,'2009/04/01',10000)
insert into tbf values(5,'2009/04/07',60000)
select datediff(dd,a.tim,b.tim),a.*
from tbf a
left join tbf b on a.id = b.id+1
drop table tbf
/*
id tim acc
----------- ----------- ---------- ---------------------------------------
NULL 1 2009/01/01 40000.0000
-31 2 2009/02/01 30000.0000
-28 3 2009/03/01 20000.0000
-31 4 2009/04/01 10000.0000
-6 5 2009/04/07 60000.0000
(5 行受影响)
*/
.....交易时间...........当前余额.....
.....(1)xxxx-xx-xx ........40000.0.....
.....(2)xxxx-xx-xx.........30000.0.....
.....(3)xxxx-xx-xx.........20000.0.....
.....(4)xxxx-xx-xx.........10000.0.....
大概是这种情况,我想求相邻两行的 交易时间 的差,注意是相邻两行,然后用40000.0×时间(2)与时间(1)的差×日利率=利息。
解决方法:
--id为(1),(2)列行字段
select datadiff(dd,a.交易时间,b.交易时间)
from tableName a
left join tableName b on a.id = b.id+1
存储过程:
declare @t table(id int,d datetime,v float)
insert into @t select 1,'2008-01-05',4000
insert into @t select 2,'2008-01-03',2000
insert into @t select 3,'2008-01-02',1000
declare @利率 float
set @利率=0.03
select a.*,a.v*datediff(d,b.d,a.d)*@利率 from @t a left join @t b on a.id=b.id-1
/*
id d v
----------- ----------------------- ---------------------- ----------------------
1 2008-01-05 00:00:00.000 4000 240
2 2008-01-03 00:00:00.000 2000 60
3 2008-01-02 00:00:00.000 1000 NULL
*/
或
create table tbf(id int,tim varchar(10),acc decimal(22,4))
insert into tbf values(1,'2009/01/01',40000)
insert into tbf values(2,'2009/02/01',30000)
insert into tbf values(3,'2009/03/01',20000)
insert into tbf values(4,'2009/04/01',10000)
insert into tbf values(5,'2009/04/07',60000)
select datediff(dd,a.tim,b.tim),a.*
from tbf a
left join tbf b on a.id = b.id+1
drop table tbf
/*
id tim acc
----------- ----------- ---------- ---------------------------------------
NULL 1 2009/01/01 40000.0000
-31 2 2009/02/01 30000.0000
-28 3 2009/03/01 20000.0000
-31 4 2009/04/01 10000.0000
-6 5 2009/04/07 60000.0000
(5 行受影响)
*/
评论: 0 | 引用: 0 | 查看次数: -
发表评论
上一篇
下一篇

文章来自:
Tags: