SELECT rq, dw jfje, dfje, ye, CASE sign(total) WHEN 1 THEN '借' WHEN 0 THEN '平' WHEN - 1 THEN '贷' END AS signflag FROM (SELECT rq, dw, jfje, dfje, (SELECT SUM(jfje) - SUM(dfje)-SUM(jkje) FROM tblcome WHERE rq<= a.rq) AS total FROM tblcome a ) b但是数据量大时,性能较差.
update a set ye = (select sum(jfje) from table where dw = a.dw and rq < a.rq) - (select sum(dfje) from table where dw = a.dw and rq < a.rq) - (select sum(jkje) from table where dw = a.dw and rq < a.rq) from table a
declare @a money set @a=0 update 表 set @a=@a+isnull(jfje,0)-isnull(dfje,0)-isnull(jkje,0),ye=@a
select rq, (select sum(jfje)+sum(dfje)+sum(jkje) from 表名 where rq<a.rq) ye from table a
select rq, (select isnull(sum(jfje),0) - isnull(sum(dfje),0)- isnull(sum(jkje),0) from 表名 where rq<a.rq) ye from table a
sorry 想用update 语句计算余额
select rq, dw,jfje,dfje,jkje, (select sum(jfje)-sum(dfje)-sum(jkje) from Tbl where rq<=a.rq) ye from Tbl a
WHEN 1 THEN '借' WHEN 0 THEN '平' WHEN - 1 THEN '贷' END AS signflag
FROM (SELECT rq, dw, jfje, dfje,
(SELECT SUM(jfje) - SUM(dfje)-SUM(jkje)
FROM tblcome
WHERE rq<= a.rq) AS total
FROM tblcome a
) b但是数据量大时,性能较差.
- (select sum(dfje) from table where dw = a.dw and rq < a.rq)
- (select sum(jkje) from table where dw = a.dw and rq < a.rq)
from table a
set @a=0
update 表 set @a=@a+isnull(jfje,0)-isnull(dfje,0)-isnull(jkje,0),ye=@a
from table a
from table a
(select sum(jfje)-sum(dfje)-sum(jkje) from Tbl where rq<=a.rq) ye
from Tbl a