Hello:
Below is code for a CTE "table" that I have for displaying for one account and one year the Opening Balance, Debit, Credit, and Ending Balance for each period of that year.
The GL10111 table in SQL, though, does not display records (i.e. periods) where there is no transaction activity. The History Summary Inquiry window in GP does show all periods, whether there are transactions or not. But, again, this table does not.
So, I'm using programming from this CTE "table" to essentially create those periods.
The problem that I'm having is that I need, for each such "created" period to show the Opening Balance and Ending Balance from the previous row.
As you can see from the screenshot below and if you look at the "Period 1" that I created, I have not been successful in doing this.
I need for 209805.93 to appear for the Opening Balance and Ending Balance of Period 1.
Can someone please let me know what I need to tweak in the programming to make this happen?
Thanks!
John
WITH y as (select 2012 as yno)
, p as (
select 0 as pno union all
select 1 as pno union all
select 2 as pno union all
select 3 as pno union all
select 4 as pno union all
select 5 as pno union all
select 6 as pno union all
select 7 as pno union all
select 8 as pno union all
select 9 as pno union all
select 10 as pno union all
select 11 as pno union all
select 12 as pno
)
SELECT
GL.ACTINDX AS [Account Index]
, y.yno AS [Year]
, p.pno AS [Period]
, isnull(CASE
WHEN p.pno = 0 THEN GL10111.PERDBLNC
ELSE SUM(-GL10111.CRDTAMNT + GL10111.DEBITAMT + PREV.PERDBLNC - GL10111.PERDBLNC) - (GL10111.DEBITAMT - GL10111.CRDTAMNT)
END,0) AS [Opening Balance]
, isnull(CASE
WHEN p.pno = 0 THEN 0
ELSE GL10111.DEBITAMT
END,0) AS [Debit]
, isnull(CASE
WHEN p.pno = 0 THEN 0
ELSE GL10111.CRDTAMNT
END,0) AS [Credit]
, isnull(CASE
WHEN p.pno = 0 THEN 0
ELSE GL10111.DEBITAMT - GL10111.CRDTAMNT
END,0) AS [Net Change]
, isnull(CASE
WHEN p.pno = 0 THEN GL10111.PERDBLNC
ELSE SUM(-GL10111.CRDTAMNT + GL10111.DEBITAMT + PREV.PERDBLNC - GL10111.PERDBLNC)
END,0) AS [Ending Balance]
, GL00105.ACTNUMST AS [Account]
, GL00100.ACTDESCR AS [Account Description]
FROM y
CROSS JOIN p
INNER JOIN (SELECT DISTINCT
ACTINDX, YEAR1
FROM GL10111
WHERE GL10111.YEAR1 = 2012
) GL ON y.yno = GL.YEAR1
INNER JOIN GL00100 ON GL.ACTINDX = GL00100.ACTINDX
INNER JOIN GL00105 ON GL.ACTINDX = GL00105.ACTINDX
LEFT JOIN GL10111 ON GL.ACTINDX = GL10111.ACTINDX
AND y.yno = GL10111.YEAR1
AND p.pno = GL10111.PERIODID
LEFT JOIN GL10111 AS PREV ON GL10111.ACTINDX = PREV.ACTINDX
AND GL10111.YEAR1 = PREV.YEAR1
AND GL10111.PERIODID >= PREV.PERIODID
GROUP BY
GL.ACTINDX
, y.yno
, p.pno
, GL10111.DEBITAMT
, GL10111.CRDTAMNT
, GL10111.PERDBLNC
, GL00105.ACTNUMST
, GL00100.ACTDESCR
ORDER BY
GL.ACTINDX
, y.yno
, p.pno
;
*This post is locked for comments