SQLTeam.com | Weblogs | Forums

Calculation of a Variance


#1

Hi I am new to SQL and was just wondering how I could insert a field to calculate the difference between a certain field in two records. For Example

Row A Variance
100 Null

Row B Variance
200 100

Great full for any help.


#2

Can you add some details? What do the tables look like and what are you expecting?


#3

Do you mean something like:

Drop Table If Exists #Test
Create Table #Test(Id Int Identity(1,1), Value Int)
Insert Into #Test(Value)
Values(100),(241),(99),(365),(45),(46),(46),(300),(252)

Select 
	Id,
	Value,
	Value - Lag(Value,1) Over (Order By Id) As Variance
From #Test