日期:2014-05-18 浏览次数:20982 次
--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 行受影响) */
------解决方案--------------------
select datadiff(dd,a.交易时间,b.交易时间) from tableName a left join tableName b on a.id = b.id+1
------解决方案--------------------