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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help Calculating Values in different rows

Author  Topic 

Charlow80
Starting Member

9 Posts

Posted - 2009-11-16 : 22:19:49
I want to subtract the value end from the first record with id 1 from the start value of the id = 2 and continue with the values from odd ids minus the even ones. Also, either update or insert the values into the diff column. Does anyone know how to do this, I am not sure if I use a cursor or it t-sql has another way of subtracting these values in different records.

Below are my first 4 records

id tid Lid start end diff
1 302297 794 100 885 0
2 302297 798 885 1295 0
3 386050 747 63690 64236 0
4 386050 750 64237 64786 0

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-11-16 : 22:52:02
[code]
select *, diff = t2.start - t1.end
from tbl t1
inner join tbl t2 on t1.id = t2.id - 1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Charlow80
Starting Member

9 Posts

Posted - 2009-11-17 : 01:10:00
Thanks, thats what I needed!
Go to Top of Page
   

- Advertisement -