日期:2014-05-19  浏览次数:20417 次

想对表中同一列的数列进行运算应该如何实现
如     第一列的第二个记录乘以二加上第一个记录         应该如何做    


------解决方案--------------------
select identity(int,1,1) as id,col1 into # from tb
select isnull((select 2*b.col1 from tb b where b.id=a.id+1) ,0)+a.col1 as sumvalue from tb a
drop table #
------解决方案--------------------
--插入临时表,并升成一自增列
select identity(int,1,1) as id,col1 into # from tb
--选出当前id的下一列col的值并剩二(最后一行没有id+1的col所以加上isnull( ' ',0))
--用下列的值乘二后加本列col1的值isnull((select 2*b.col1 from tb b where b.id=a.id+1) ,0)+a.col1
--执行
select isnull((select 2*b.col1 from tb b where b.id=a.id+1) ,0)+a.col1 as sumvalue from tb a
--删除临时表
drop table #