Click here to Skip to main content
15,916,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI,

SQL
SELECT  MONTH(Month) AS Month ,
         SUM(vlera) AS Value ,

         SUM(0) AS [Don't know how to do]
 FROM    dbo.tblTest
 GROUP BY MONTH(Month)

Month       Value                               Don't know how to do
----------- -------------                        --------------------
1           12.50                                   0
2           12.50                                   0
3           12.50                                   0
4           13.00                                   0

Could anybody let me know how to select sum of second and third columns in different rows, in this example the third column should contain:
12.5
25
37.5
50.5


Thanx in advance
Posted
Updated 10-Jan-12 4:05am
v3

Try using "apply" operator

SQL
SELECT  
	t.Month AS Month ,
    	t.vlera AS Value ,
	tt.column_total AS [sub total] 
FROM    dbo.tblTest t 
cross apply 
	(select sum(vlera) as column_total
	from tblTest
	) as tt 
ORDER BY t.Month


also see the following link for different options:

http://stackoverflow.com/questions/860966/calculate-a-running-total-in-sqlserver[^]
 
Share this answer
 
try with this query,

SQL
  SELECT a.month,
       a.value,
       SUM(b.value) as cumulativesum
FROM #temp a,
 #temp b
where (b.month <= a.month)
GROUP BY a.month,a.value




regards

pal
 
Share this answer
 
v2

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