Hi,
I am trying to create a running balance for 12 months, and have created the code below for reference. My query table is based off of a summary table (drill_sum), which totals all activity per month for each account number. This query works correctly, however I would like to include a balance for those months where there is no activity. If you look below, I would like to create a balance for 2008M07 to 2008M10. These are the months that have no activity and are not within the summary table. Thank you in advance for your help. Much appreciated!!
Current Results:
Period entityid acctnum dept jobcode total run. Total
2008M04 3305 1270-0000 0 0 -85.83 -85.83
2008M05 3305 1270-0000 0 0 -85.83 -171.66
2008M06 3305 1270-0000 0 0 -85.84 -257.5
2008M11 3305 1270-0000 0 0 591.38 333.88
2008M12 3305 1270-0000 0 0 -84.49 249.39
2009M01 3305 1270-0000 0 0 -84.48 164.91
2009M02 3305 1270-0000 0 0 -84.48 80.43
2009M03 3305 1270-0000 0 0 -84.48 -4.05
Desired Results:
Period entityid acctnum dept jobcode total run. total
2008M04 3305 1270-0000 0 0 -85.83 -85.83
2008M05 3305 1270-0000 0 0 -85.83 -171.66
2008M06 3305 1270-0000 0 0 -85.84 -257.5
2008M07 3305 1270-0000 0 0 0 -257.5
2008M08 3305 1270-0000 0 0 0 -257.5
2008M09 3305 1270-0000 0 0 0 -257.5
2008M10 3305 1270-0000 0 0 0 -257.5
2008M11 3305 1270-0000 0 0 591.38 333.88
2008M12 3305 1270-0000 0 0 -84.49 249.39
2009M01 3305 1270-0000 0 0 -84.48 164.91
2009M02 3305 1270-0000 0 0 -84.48 80.43
2009M03 3305 1270-0000 0 0 -84.48 -4.05
select a.Period,
a.entityid,
a.acctnum,
a.department,
a.jobcode,
a.total,
(select sum(b.total) from dbo.drill_sum b
where left(b.period,4) + right(b.period, 2) <= left(a.period, 4) + right(a.period,2)
and a.acctnum = b.acctnum
and a.entityid = b.entityid
and a.department = b.department
and a.jobcode = b.jobcode
and b.PERIOD between '2008m04' and '2009m03' ) as 'running total'
FROM dbo.drill_sum a
WHERE a.entityid in ('3305','3662','F506','F523')
and a.period between '2008M04' and '2009M03'
and a.ACCTNUM < '5000-0000'
ORDER BY a.ENTITYID, a.ACCTNUM, a.PERIOD, a.DEPARTMENT, a.jobcode
I am trying to create a running balance for 12 months, and have created the code below for reference. My query table is based off of a summary table (drill_sum), which totals all activity per month for each account number. This query works correctly, however I would like to include a balance for those months where there is no activity. If you look below, I would like to create a balance for 2008M07 to 2008M10. These are the months that have no activity and are not within the summary table. Thank you in advance for your help. Much appreciated!!
Current Results:
Period entityid acctnum dept jobcode total run. Total
2008M04 3305 1270-0000 0 0 -85.83 -85.83
2008M05 3305 1270-0000 0 0 -85.83 -171.66
2008M06 3305 1270-0000 0 0 -85.84 -257.5
2008M11 3305 1270-0000 0 0 591.38 333.88
2008M12 3305 1270-0000 0 0 -84.49 249.39
2009M01 3305 1270-0000 0 0 -84.48 164.91
2009M02 3305 1270-0000 0 0 -84.48 80.43
2009M03 3305 1270-0000 0 0 -84.48 -4.05
Desired Results:
Period entityid acctnum dept jobcode total run. total
2008M04 3305 1270-0000 0 0 -85.83 -85.83
2008M05 3305 1270-0000 0 0 -85.83 -171.66
2008M06 3305 1270-0000 0 0 -85.84 -257.5
2008M07 3305 1270-0000 0 0 0 -257.5
2008M08 3305 1270-0000 0 0 0 -257.5
2008M09 3305 1270-0000 0 0 0 -257.5
2008M10 3305 1270-0000 0 0 0 -257.5
2008M11 3305 1270-0000 0 0 591.38 333.88
2008M12 3305 1270-0000 0 0 -84.49 249.39
2009M01 3305 1270-0000 0 0 -84.48 164.91
2009M02 3305 1270-0000 0 0 -84.48 80.43
2009M03 3305 1270-0000 0 0 -84.48 -4.05
select a.Period,
a.entityid,
a.acctnum,
a.department,
a.jobcode,
a.total,
(select sum(b.total) from dbo.drill_sum b
where left(b.period,4) + right(b.period, 2) <= left(a.period, 4) + right(a.period,2)
and a.acctnum = b.acctnum
and a.entityid = b.entityid
and a.department = b.department
and a.jobcode = b.jobcode
and b.PERIOD between '2008m04' and '2009m03' ) as 'running total'
FROM dbo.drill_sum a
WHERE a.entityid in ('3305','3662','F506','F523')
and a.period between '2008M04' and '2009M03'
and a.ACCTNUM < '5000-0000'
ORDER BY a.ENTITYID, a.ACCTNUM, a.PERIOD, a.DEPARTMENT, a.jobcode