SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SUM of previous row value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

DeNam
Starting Member

Sweden
15 Posts

Posted - 08/23/2013 :  03:36:35  Show Profile  Reply with Quote
Hi,

I want to have a field (T1.Field2) which sums the previous values of the same row and previous rows of another field (T1.Field1).

How do i code this in sql script.

Example

Field1 Field2
6 6
7 13
3 16

and so on.

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 08/23/2013 :  04:30:04  Show Profile  Reply with Quote
Provide us the sample data and expected output.....
Is there any datetime column/autoincremented column in the table?

--
Chandu
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22765 Posts

Posted - 08/26/2013 :  07:01:54  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Search for Running Total and you will find lot of related links

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sivadss2007
Starting Member

India
18 Posts

Posted - 08/28/2013 :  05:08:55  Show Profile  Reply with Quote
This will work fine when you have an autoincrement column.

Here in the below case id is an identity column

select t1.Field1 , SUM(t2.Field1) as field2
from @t t1
inner join @t t2 on t1.id >= t2.id
group by t1.id, t1.Field1
order by t1.id

P.Siva

Edited by - sivadss2007 on 08/28/2013 05:10:23
Go to Top of Page

ShivaKrishna
Starting Member

India
20 Posts

Posted - 08/28/2013 :  05:53:52  Show Profile  Reply with Quote
select t1.Field1 , SUM(t2.Field1) as Field2
from @t t1
inner join @t t2 on t1.id >= t2.id
group by t1.Field1
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000