Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating differences every two rows

Status
Not open for further replies.

briangriffin

Programmer
Nov 6, 2008
878
US
If possible, I'd like to calculate the differences between every two rows (bold rows to be added):

9/2022 1000
9/2021 900
Variance 100
10/2022 1000
10/2021 1200
Variance (200)

I'm guessing I would use lead/lag and some kind of loop, but if anyone has something similar I'd be very interested to see how you did it. TIA.
 
How do you know which row is first and which is second?


Borislav Borissov
VFP9 SP2, SQL Server
 
So I assume you're doing a comparison of month by month, year over year? So you want to see for January this year vs last year, Feb for this year vs last year, etc?

As far as how to make it all work, here's an idea from another discussion:

(Not trying to point elsewhere, just happened to be one I found on the topic).

Here's what I searched for to get to that end: sql server aggregation difference between 2 row. Use that or similar if you want to find other examples. I've used both methods discussed in that thread in the past, myself. I think they'll both work.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top