2

Is it possible to get compounded sum in sql server? e.g.

Month Salary Total

Jan 1000 1000

Feb 1200 2200

Mar 1000 3200

. . .

. . .

Please help.

4
  • which version of sql server Commented Mar 10, 2014 at 11:57
  • What have you tried? If you have anything lower than SQL 2012 this might be a good place to start. pawlowski.cz/2010/09/… Commented Mar 10, 2014 at 12:07
  • @VijaykumarHadalgi It doesnot have any identity column Commented Mar 10, 2014 at 12:14
  • then how'd you define a primary key on this ? Commented Mar 10, 2014 at 12:18

2 Answers 2

3
Declare @t table( Months varchar(10), Salary int) insert into @t select 'Jan', 1000 union all select 'Feb', 1200 union all select 'Mar', 1000 ;With CTE as ( select *,ROW_NUMBER()over(order by (select null))rn from @t ) ,CTE1 as ( select a.*,salary [Total] from CTE a where rn=1 union all select a.*,a.Salary+Total from CTE a inner join CTE1 b on a.rn-b.rn=1 ) select * from cte1 
Sign up to request clarification or add additional context in comments.

Comments

2

if the table structure's like this table(id int identity(1,1),month varchar(10),salary int,total int)

then you could try :

select *,(select sum(salary) from table b where b.id<=a.id) as total from table a 

DEMO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.