Quantcast
Channel: Microsoft Dynamics GP
Viewing all articles
Browse latest Browse all 38722

Forum Post: RE: T-SQL: Displaying Data from a Previous Row

$
0
0
Hi John, I've done something similar to what you're describing, and am happy to share my script here. I used the SY40102 table to get the period list from 1 to 12, and created a CTE to get all combinations of period and GL. It uses LAG, so it probably won't work on SQL versions older than 2012. I hope you find this useful. declare @theyear int set @theyear = 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 = @theyear ) , --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

Viewing all articles
Browse latest Browse all 38722

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>