Select a.凭证号,a.工号,a.日期,a.增加,a.减少,
余额=(Select IsNull(Sum(a.增加),0)-IsNull(Sum(a.减少),0) From 表 as b
where b.日期<a.日期 and b.工号=a.工号)+isNull(a.增加,0)-IsNull(a.减少,0)
From 表名 as a where SubString(a.日期,1,2)='03'搞定 :)
余额=(Select IsNull(Sum(a.增加),0)-IsNull(Sum(a.减少),0) From 表 as b
where b.日期<a.日期 and b.工号=a.工号)+isNull(a.增加,0)-IsNull(a.减少,0)
From 表名 as a where SubString(a.日期,1,2)='03'搞定 :)
Select a.凭证号,a.工号,a.日期,a.增加,a.减少,
余额=(Select IsNull(Sum(b.增加),0)-IsNull(Sum(b.减少),0) From 表名 as b
where b.日期<a.日期 and b.工号=a.工号)+isNull(a.增加,0)-IsNull(a.减少,0)
From 表名 as a where SubString(a.日期,1,2)='03'
select @month = '03'
select identity(int,1,1) as xh,a.凭证号,a.工号,a.日期,a.增加,a.减少,
(isnull(a.增加,0) - isnull(a.减少,0)) as 余额 into #temptb
from tablename a order by a.日期,a.工号select A.凭证号,A.工号,A.日期,A.增加,A.减少,
A.余额 + isnull((select 余额 from #temptb where xh = A.xh -1),0)
from #temptb A where SubString(A.日期,1,2)= @monthdrop table #temptb
select *,(增加-减少)+isnull (( select sum( 增加-减少) from 表 where 证号<aa.证号 and 工号=aa.工号 ),0)
from 表 aa
where month(日期)=3
from 表 aa
where not exists(select 1 from 表 where 工号=aa.工号 and 证号>aa.证号)
from 表 aa
where not exists(select 1 from 表 where 工号=aa.工号 and 证号>aa.证号) and month(日期)=3