日期:2014-05-18 浏览次数:20528 次
--> 测试数据: @t declare @t table ( id int ,A varchar(4),B varchar(4),C varchar(2),gs varchar(8),gsvalue varchar(500)) insert into @t SELECT 1, 5,7,'C1','A+B',null union all select 2,3,15,'C2','C1+A+B',null union all select 3,9,24,'C3','C2+A+B',null UPDATE @t SET gsvalue=replace(replace(gs,'A',A),'B',B) DECLARE @C1 varchar(20) SELECT @C1=cast(gsvalue AS varchar(200)) FROM @t WHERE C='C1'; UPDATE @t SET gsvalue=replace(gsvalue,'C1',@C1) DECLARE @C2 varchar(20) SELECT @C2=cast(gsvalue AS varchar(200)) FROM @t WHERE C='C2'; UPDATE @t SET gsvalue=replace(gsvalue,'C2',@C2) DECLARE @CNumber varchar(200) SELECT gsvalue FROM @t;
------解决方案--------------------
正在关注中
楼上的写的不错呦!
支持
------解决方案--------------------
如果确认B字段是递增的话,可以这么写
select ata.a,ata.b,ata.a+ata.b+isnull((select sum(a+b) from Test where b<ata.b),0) from Test ata
------解决方案--------------------
加个ID字段吧
declare @table table(id int,A int,B int,C int) insert into @table select 1,5,7,5+7 union select 2,3,15,13+15 union select 3,9,24,9+24 --得出结果 select tb1.a ,tb1.b ,(select sum(tb2.c) from @table tb2 where (tb2.id)<=(tb1.id) ) as cc from @table tb1 --结果 -- 5,7,12 -- 3,15,40 -- 9,24,73
------解决方案--------------------
对不起,刚才一冲动,rollup是不能满足楼主的需求的,不过采用一下语句就可以
SELECT PMC1QTY, PMC2QTY, SUM(PMC1QTY + PMC2QTY) OVER(ORDER BY ROWNUM) FROM SCM_DRP_PMC WHERE PMC1QTY != 0 OR PMC2QTY != 0
------解决方案--------------------
update t1 set c=(a*1 + b + case when (select top 1 c from table where id=t1.id-1 order by id desc) is not null then (select top 1 c from table where id=t1.id-1 order by id desc) else then 0 end ) from table t1