日期:2014-05-18  浏览次数:20402 次

一个计算的sql语句
SQL code
测试数据如下:
mom  day   zy           jfje   dfje   jeye(金额余额)
01   01    期初余额      0      0     100000000
01   02   预收账款      1000      0     0
01   02   预收账款      1000      0     0
01   02   预收账款      1000      0     0
01   02   预收账款      1000      0     0
01   02   预收账款      1000      0     0
01   02   预收账款      1000      0     0
01   02   预收账款      0      12330     0

jeye的值要等于   期初余额的jeye 的值 +jfje-dfje
结果
mom  day   zy           jfje   dfje   jeye(金额余额)
01   01    期初余额      0      0     100000000
01   02   预收账款      1000      0   99999000
....
....
....
本人用一下写法能实现 不过数据一多 效率就很差 求大牛指点 
select mom,day, zy,jfje,dfje,jeye=(select jeye=sum(jeye)+sum(jfje)-sum(dfje) from #result where rid<=a.rid) from (select * ,row_numbe() over(order by getdate()) rid from #result) a
求更高效率的语句  [img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/15.gif][/img]



------解决方案--------------------
SQL code

declare @t table
([mom] varchar(2),[day] varchar(2),[zy] varchar(8),[jfje] int,[dfje] int,[jeye] int)
insert @t
select '01','01','期初余额',0,0,100000000 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',0,12330,0

;with maco as 
(
select row_number() over(order by getdate()) rid,* from @t
)
select mom,[day],zy,jfje,dfje,jeye=
(select sum(jeye-jfje+dfje) from maco where rid<=t.rid) from maco t
/*
mom  day  zy       jfje        dfje        jeye
---- ---- -------- ----------- ----------- -----------
01   01   期初余额     0           0           100000000
01   02   预收账款     1000        0           99999000
01   02   预收账款     1000        0           99998000
01   02   预收账款     1000        0           99997000
01   02   预收账款     1000        0           99996000
01   02   预收账款     1000        0           99995000
01   02   预收账款     1000        0           99994000
01   02   预收账款     0           12330       100006330
*/

--你这个表没有主键吗?

------解决方案--------------------
SQL code
看下这个能否满足要求
with cte1 as(select row=row_number() over(order by mom,day),* from tb)
, cte2 as(select * from cte1 where row=1
          union all
          select cte1.row,cte1.mom,cte1.day,cte1.zy,cte1.jfje,cte1.dfje,jeye=cte2.jeye+cte1.jfje-cte1.dfje
          from cte2 join cte1 on cte1.row=cte2.row+1)

select * from cte2

1    1    1    期初余额    0    0    100000000
2    1    2    预收账款    1000    0    100001000
3    1    2    预收账款    1000    0    100002000
4    1    2    预收账款    1000    0    100003000
5    1    2    预收账款    1000    0    100004000
6    1    2    预收账款    1000    0    100005000
7    1    2    预收账款    1000    0    100006000
8    1    2    预收账款    0    12330    99993670

------解决方案--------------------
SQL code

create table #result
([mom] varchar(4),[day] varchar(4),[zy] varchar(8),[jfje] int,[dfje] int,[jeye] int)

insert #result
select '01','01','期初余额',0,0,100000000 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',1000,0,0 union all
select '01','02','预收账款',0,12330,0


;with t as
(select mom,[day],zy,jfje,dfje,jeye,
 row_number() over(order by getdate()) rn from #result)
select mom,[day],zy,jfje,dfje,c.ye
from t a
cross apply
(select sum(jeye)-sum(jfje)+sum(dfje) 'ye' from t b
 where b.rn<=a.rn) c
 
/*
mom  day  zy       jfje        dfje        ye
---- ---- -------- ----------- -----------