Hi John. Yes you can have the year (or list of years) hard-coded into the script. The code below gives you data for years 2012 and 2013. If you just want a single year, you can edit the WHERE clause to: where p.series = 0 and p.year1 = 2012 --CTE get every account and periodid combination for the year 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(2012,2013) ) , --CTE get period movement with running total of net change 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 , sum(isnull(g.perdblnc,0)) over(partition by isnull(g.actindx,p.actindx) order by isnull(g.actindx,p.actindx), p.year1, p.periodid ) as ClosingBalance from allaccounts p left outer join gl10111 g on p.YEAR1 = g.YEAR1 and p.periodid = g.periodid and p.actindx = g.actindx) --final select with lag for opening balance: previous cb = ob select actindx, year1, periodid, debitamnt, crdtamnt, netchange, lag(closingbalance) over(partition by actindx order by actindx, year1, periodid) as OpeningBalance, ClosingBalance from allbalances order by actindx, year1, periodid
↧