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 Roger: Actually, I took this code a step further, in order to show data for more than one fiscal year and specifically for all years greater than 2010. When I reviewed the results, it actually turned out to be not what I need after all. :( Below is the code. The problem is that the CROSS APPLY, where it is summing GL10111's PERDBLNC, needs to reset after one year has passed. This regards the following clause: CROSS APPLY (SELECT SUM(netchange) AS NetChange For each new year, the "Opening Balance" for Period 0 needs to be 0, while the "Ending Balance for Period 0 should be the Net Change (i.e. the Debit minus the Credit). And, the "Opening Balance" for Period 1 for the new year should simply bring over the prior period's "Ending Balance". And, Period 1's "Ending Balance" should be its "Opening Balance" combined with the period's "Net Change". This math needs to continue, for each succeeding period, until the next new year when everything should "reset" again. Instead, the query is summing the PERDBLNC for each account across each year, rather than resetting for the new year. (I hope that makes sense.) I tried to fix this myself by doing a "GROUP BY" on YEAR1. But, when I run the query, it is still continuing to run without any results returned. I imagine that it's going to timeout eventually. I'm sorry. I should have mentioned that I was planning on using this for more than one fiscal year. I confined it to one year, to keep the initial results returned simplified, until I could get working code to use for more than one year. Please let me know if and how this is possible in SQL 2008. We're close. We just need to get this to work for more than one fiscal year. Thanks, Roger! John WITH allbalances AS ( SELECT ISNULL(g.ACTINDX,p1.ACTINDX) AS ACTINDX ,p1.YEAR1 ,p1.PERIODID ,ISNULL(g.DEBITAMT,0) AS debitamnt ,ISNULL(g.CRDTAMNT,0) AS crdtamnt ,ISNULL(g.PERDBLNC,0) AS netchange ,p1.PERIODDT FROM (SELECT g.ACTINDX ,p.PERIODID ,p.YEAR1 ,p.PERIODDT FROM SY40100 p CROSS JOIN GL00100 g WHERE p.SERIES = 0 AND p.YEAR1 > 2010 --IN (2012) AND ACTINDX = 1 ) p1 LEFT OUTER JOIN GL10111 g ON p1.YEAR1 = g.YEAR1 AND p1.PERIODID = g.PERIODID AND p1.ACTINDX = g.ACTINDX ) select g.ACTNUMST AS [Account Number] ,d.ACTDESCR as [Account Description] ,a.YEAR1 As Year ,a.PERIODID as Period ,b.NetChange - a.netchange as [Opening Balance] ,a.debitamnt AS [Debit] ,a.crdtamnt AS [Credit] ,a.netchange as [Net Change] ,b.NetChange as [Ending Balance] FROM allbalances a inner JOIN GL00105 g on a.ACTINDX = g.ACTINDX inner JOIN GL00100 d on a.ACTINDX = d.ACTINDX CROSS APPLY (SELECT SUM(netchange) AS NetChange FROM allbalances b WHERE a.ACTINDX=b.ACTINDX AND b.PERIODDT <= a.PERIODDT) b ORDER BY g.ACTNUMST,a.YEAR1,a.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>