Author |
Topic |
lnmhelp
Starting Member
4 Posts |
Posted - 2007-10-11 : 14:04:16
|
Hello,Here is a situation I have come across. I need a stored procedure that can calculate running totals for a fiscal year starting october. This procedure takes a fiscal year and costcenter as user input: like 2003-2004 which is a varchar field and 123456789 as costcenter which is also a varchar field. My table is made up of Fiscal Year,CostCenter, Month, Fiscal Month, Cost, Fiscal Year Total. (Fiscal Month = 1 when Month = 10 for a given Fiscal Year)Fiscal Year Total is the running total field. How do I accomplish this? Thanks in advance! |
|
Kristen
Test
22859 Posts |
|
lnmhelp
Starting Member
4 Posts |
Posted - 2007-10-11 : 15:32:13
|
Thank you Kirsten,I will read up and then post. Appreciate your candor. |
 |
|
lnmhelp
Starting Member
4 Posts |
Posted - 2007-10-12 : 09:53:48
|
Here is my test data. Any feedback in this matter is appreciated. This is my table:CREATE TABLE MyTable ( CostCenter varchar (7) NOT NULL , FiscalYear varchar (9) NOT NULL , Month tinyint NULL , Cost money NULL , FiscalMonth tinyint NULL , FiscalYearTotal money NULL ) Here are some values:INSERT INTO MyTable(CostCenter, FiscalYear, Month, Cost, FiscalMonth, FiscalYearTotal)select '1111111','2004-2005',3,125.0000,6,.0000union allselect '1111111','2004-2005',4,150.0000,7,.0000union allselect '1111111','2005-2006',12,200.0000,3,.0000union allselect '1111111','2004-2005',5,55.0000,8,.0000union allselect '1111111','2004-2005',9,80.0000,12,.0000union allselect '5555555','2005-2006',10,55.0000,1,55.0000union allselect '5555555','2004-2005',10,100.0000,1,100.0000union allselect '4444444','2005-2006',10,120.0000,1,120.0000union allselect '5555555','2005-2006',6,140.0000,9,.0000union allselect '5555555','2005-2006',5,65.0000,8,.0000union allselect '5555555','2005-2006',4,75.0000,7,.0000union allselect '5555555','2005-2006',3,85.0000,6,.0000union allselect '5555555','2005-2006',2,45.0000,5,.0000union allselect '5555555','2005-2006',1,65.0000,4,.0000union allselect '5555555','2005-2006',7,95.0000,10,.0000union allselect '5555555','2005-2006',8,145.0000,11,.0000union allselect '5555555','2005-2006',9,155.0000,12,.0000union allselect '5555555','2005-2006',11,35.0000,2,.0000union allselect '5555555','2005-2006',12,750.0000,3,.0000union allselect '5555555','2004-2005',12,109.0000,3,.0000Expected Result:Select CostCenter, FiscalYear, Month, Cost, FiscalMonth, FiscalYearTOtal from MyTable where CostCenter = '5555555' and FiscalYear = '2005-2006'CostCenter FiscalYear Month Cost FiscalMonth FiscalyearTotal========================================================================================5555555 2005-2006 10 55.0000 1 55.00005555555 2005-2006 6 140.0000 9 1315.00005555555 2005-2006 5 65.0000 8 1175.00005555555 2005-2006 4 75.0000 7 1110.00005555555 2005-2006 3 85.0000 6 1035.00005555555 2005-2006 2 45.0000 5 950.00005555555 2005-2006 1 65.0000 4 905.00005555555 2005-2006 7 95.0000 10 1410.00005555555 2005-2006 8 145.0000 11 1555.00005555555 2005-2006 9 155.0000 12 1710.00005555555 2005-2006 11 35.0000 2 90.00005555555 2005-2006 12 750.0000 3 840.0000 |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-10-12 : 10:06:28
|
[code]Select CostCenter, FiscalYear, Month, Cost, FiscalMonth, [FiscalYearTOtal] = ( SELECT SUM(Cost) FROM MyTable AS T2 WHERE T2.CostCenter = T1.CostCenter AND T2.FiscalYear = T1.FiscalYear -- Select earlier fiscal months, and self, only AND T2.FiscalMonth <= T1.FiscalMonth )from MyTable AS T1where CostCenter = '5555555' and FiscalYear = '2005-2006'[/code]A JOIN to a nested Sub-Select, containing an Aggregate, might be more efficient if you will be outputting LOTs of rows in the ResultSet.Kristen |
 |
|
lnmhelp
Starting Member
4 Posts |
Posted - 2007-10-12 : 11:59:36
|
Thanks Kristen, Works great. |
 |
|
|
|
|