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.
| Author |
Topic |
|
granth
Starting Member
1 Post |
Posted - 2010-09-22 : 18:38:55
|
| Hi ThereI 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 revenueshere 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.CUSTNAMEFROM dbo.GH_SUMMARY_SALES_ANALYSIS INNER JOINdbo.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.CUSTNMBRGROUP 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.CUSTNAMEORDER 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 SQLThanksGrant |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-23 : 03:01:25
|
| DELETEDThanksRohit |
 |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-23 : 03:12:01
|
Hi granthI 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 salesJan 100Feb 50Mar 200Apr 20May 600Jun 140following 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_SalesDECLARE @count INTDECLARE @max_count INTDECLARE @total_till_current_month INTSET @total_till_current_month = 0SET @count = 1SELECT @max_count = MAX(id) FROM #tempWHILE(@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 +1ENDSELECT * FROM #temp and my o/p with my sample data is:id month sales running_total1 Jan 100 1002 Feb 50 1503 Mar 200 3504 Apr 20 3705 May 600 9706 Jun 140 1110hope this is what you want to do. go through the above code and modify your's accordingly.ThanksRohit |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|
|
|