sql问题,急``请哪位大吓帮忙
已知有A,B列求C列,如下:
A,B,C
5,7,C1=A1+B1
3,15 C2=C1+A2+B2
9,24 C3=C2+A3+B3
.. ..
要生成C这一列,求解(只能用标准SQL99语句实现)用一句SQL完成(可以复合或嵌套语句来完成)
不能用其他的指针或者循环语句的方式,因为题目的要求是对象的整体进行一次性的运算而不是一次次对其分量做运算`
[解决办法]
c_n=(select sum([A])+sum([B])from [tab] where [c]<=C_n)
我看C的值就是小于等于该行记录的所有A和B的值的和。不晓得对不对?
[解决办法]
关注
[解决办法]
关注
[解决办法]
--> 测试数据: @tdeclare @t table ( id int ,A varchar(4),B varchar(4),C varchar(2),gs varchar(8),gsvalue varchar(500))insert into @tSELECT 1, 5,7,'C1','A+B',null union allselect 2,3,15,'C2','C1+A+B',null union allselect 3,9,24,'C3','C2+A+B',nullUPDATE @tSET 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 @tSET gsvalue=replace(gsvalue,'C1',@C1)DECLARE @C2 varchar(20)SELECT @C2=cast(gsvalue AS varchar(200)) FROM @t WHERE C='C2';UPDATE @tSET 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 @tableselect 1,5,7,5+7unionselect 2,3,15,13+15unionselect 3,9,24,9+24--得出结果select tb1.a ,tb1.b ,(select sum(tb2.c) from @table tb2 where (tb2.id)<=(tb1.id) ) as ccfrom @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