Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Running totals HELP!

Author  Topic 

granth
Starting Member

1 Post

Posted - 2010-09-22 : 18:38:55
Hi There
I need some help with a view I created, it currently displays the the month by month revenue and budgets figures. What I require is a way to create a cumualtive total for each month e.g. January = Jan but Feb = Feb + Jan revenues, March = Mar + Feb + Jan revenues
here is my current view
SELECT dbo.GH_SUMMARY_SALES_ANALYSIS.CUSTNMBR, dbo.GH_SUMMARY_SALES_ANALYSIS.MonthName,
dbo.GH_SUMMARY_SALES_ANALYSIS.[Fiscal Month], dbo.GH_SUMMARY_SALES_ANALYSIS.[Fiscal Year],
SUM(dbo.GH_SUMMARY_SALES_ANALYSIS.DELTAREVENUES) AS DELTAREVENUES,
dbo.GH_DCL_BUDGET.January,
dbo.GH_DCL_BUDGET.DCL_Functional_Monthly_S_1, dbo.GH_DCL_BUDGET.YEAR1,
dbo.RM00101.CUSTNAME
FROM dbo.GH_SUMMARY_SALES_ANALYSIS INNER JOIN
dbo.GH_DCL_BUDGET ON dbo.GH_SUMMARY_SALES_ANALYSIS.CUSTNMBR = dbo.GH_DCL_BUDGET.CUSTNMBR AND dbo.GH_SUMMARY_SALES_ANALYSIS.MonthName = dbo.GH_DCL_BUDGET.January dbo.RM00101 ON dbo.GH_SUMMARY_SALES_ANALYSIS.CUSTNMBR = dbo.RM00101.CUSTNMBR
GROUP BY dbo.GH_SUMMARY_SALES_ANALYSIS.CUSTNMBR, dbo.GH_SUMMARY_SALES_ANALYSIS.[Fiscal Month], dbo.GH_SUMMARY_SALES_ANALYSIS.[Fiscal Year],
dbo.GH_SUMMARY_SALES_ANALYSIS.MonthName, dbo.GH_DCL_BUDGET.January, dbo.GH_DCL_BUDGET.DCL_Functional_Monthly_S_1,
dbo.GH_DCL_BUDGET.YEAR1, dbo.GH_DCL_BUDGET.CUSTNMBR, dbo.RM00101.CUSTNAME
ORDER BY dbo.GH_SUMMARY_SALES_ANALYSIS.CUSTNMBR, dbo.GH_SUMMARY_SALES_ANALYSIS.[Fiscal Month]

I am really hoping someone can help me summarize the data into cumulative months, I have marked the fields that need to be summarized in red.
I thank anyone who can provide sone help I am fairly new to SQL
Thanks
Grant

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-23 : 03:01:25
DELETED

Thanks
Rohit
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-23 : 03:12:01
Hi granth

I have got one sample query here which you can adapt for your need.

I am having a sample table Table_Monthly_Sales having month & sales as columns with the following data:

month sales
Jan 100
Feb 50
Mar 200
Apr 20
May 600
Jun 140

following is the code to get the running total:


CREATE TABLE #temp
(
id INT IDENTITY(1,1),
month VARCHAR(50),
sales INT,
running_total INT
)

INSERT INTO #temp(month,sales)
SELECT month,sales FROM Table_Monthly_Sales

DECLARE @count INT
DECLARE @max_count INT
DECLARE @total_till_current_month INT

SET @total_till_current_month = 0
SET @count = 1
SELECT @max_count = MAX(id) FROM #temp

WHILE(@count <= @max_count)
BEGIN

DECLARE @sales INT

SELECT @sales = sales FROM #temp WHERE id = @count
SET @total_till_current_month = @total_till_current_month + @sales

UPDATE #temp
SET running_total = @total_till_current_month
WHERE id = @count

SET @count = @count +1
END

SELECT * FROM #temp


and my o/p with my sample data is:

id month sales running_total
1 Jan 100 100
2 Feb 50 150
3 Mar 200 350
4 Apr 20 370
5 May 600 970
6 Jun 140 1110

hope this is what you want to do. go through the above code and modify your's accordingly.

Thanks
Rohit
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-23 : 12:27:43
see scenario 1 here

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -