Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Suppose I have a table (named MyIncome) with 3 columns, the first is Year (2008, 2008, 2009), the second is Month (1, 2, 1), the third is Income ($1000, $1500, $2000)
Now I want to know how much the Income are for each year in the Year column. I used the following T-SQL statement:
SELECT Year, sum(Income) as [Annual Income] FROM MyIncome GROUP BY Year
The result should be a table with two columns, one is Year(2008, 2009), the other is Annual Income ($2500,$2000).
But it didn't work unless I had to group by both Year and Income (this will cause the result to be different from what I want with grouping by Year only).
Especially the above statement seems to work well in Access, could you please give me a solution for my problem, I think it is very often to do such a job.
Thank you so much!
UPDATE
I'm sorry for the mistake, please see another problem of mine, now I want to know the income up to each year (for example, for the MyIncome table above, the result should be a table with two columns: Year(2008, 2009) and Total Income(2500, 4500)). The income up to 2009 is the sum of 2008's income and 2009's income.
Please help me solve that.
Thank you so much!
Posted
Updated 25-May-11 20:01pm
v3

1 solution

Can you have a look at the code given below:
USE AdventureWorks2008R2;
GO
SELECT SalesOrderID, SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail AS sod
GROUP BY SalesOrderID
ORDER BY SalesOrderID;


Link is: http://msdn.microsoft.com/en-us/library/ms177673.aspx[^]

Examples using group by clause.

Hope this will help you.

I tested your code:
SELECT [Year], sum(Income) as [Annual Income] FROM dbo.Table_1 GROUP BY [Year]


Out put is:
2008 2500
2009 2000

So no problem with it.

You are absolutely correct.

Updated Answer:

Please check the code given below:

SQL
DECLARE @TableVar TABLE
(
    YearWise        BIGINT,
    TotalAmount     BIGINT
)
INSERT INTO @TableVar
SELECT [Year], sum(Income) as [Annual Income] FROM dbo.Table_1 GROUP BY [Year]
SELECT YearWise,(SELECT SUM(TotalAmount)
FROM @TableVar
WHERE TotalAmount>=t.TotalAmount) AS CumulativeSum
FROM @TableVar t


Output:

YearWise CumulativeSum
2008 2500
2009 4500

It will work fine.
Hope this will help you.
Cheers man..
 
Share this answer
 
v6
Comments
[no name] 26-May-11 2:08am    
I'm sorry for the mistake about "The true problem", it seems that I was joking! Please see my updated question to help me solve another problem!
Thank you so much!
arindamrudra 26-May-11 2:35am    
So you want the cumulative sum.
arindamrudra 26-May-11 2:46am    
Check my updated answer.
arindamrudra 26-May-11 2:50am    
Look for other solutions too. There may be another good solution for that.
arindamrudra 26-May-11 2:53am    
Please check this link:
http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/75477b69-41e5-4b49-8a13-c578a803c66d

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900