Yep, both those errors will be due to the SQL version. Try this one. It uses a # temp table. I tried it with using CTE's only, but it was VERY slow. with allaccounts as( select g.actindx, p.periodid, p.year1 from sy40102 p cross join gl00100 g where p.series = 0 and p.year1 in(2013,2014) ) , allbalances as( select isnull(g.actindx,p.actindx) as ACTINDX, p.year1, p.periodid, isnull(g.debitamt,0) as debitamnt, isnull(g.crdtamnt,0) as crdtamnt, isnull(g.perdblnc,0) as netchange from allaccounts p left outer join gl10111 g on p.YEAR1 = g.YEAR1 and p.periodid = g.periodid and p.actindx = g.actindx) select a.actindx, a.year1, a.periodid, a.debitamnt, a.crdtamnt, a.netchange into #temptable from allbalances a order by actindx, year1, periodid; with ctebalances as( select a.* , (select SUM(b.netchange) from #temptable b where b.PERIODID <= a.PERIODID and b.YEAR1 = a.YEAR1 and b.ACTINDX = a.ACTINDX) as ClosingBalance from #temptable a) select a.ACTINDX, g.ACTNUMST, a.year1, a.periodid, a.debitamnt, a.crdtamnt, (select b.ClosingBalance from ctebalances b where b.PERIODID = a.PERIODID-1 and b.YEAR1 = a.YEAR1 and b.ACTINDX = a.ACTINDX) as OpeningBalance, a.netchange, a.ClosingBalance from ctebalances a inner join GL00105 g on a.ACTINDX = g.ACTINDX order by ACTINDX, YEAR1, PERIODID
↧