troubleshooting Question

Subtract values between rows

Avatar of ibradshaw
ibradshaw asked on
MySQL Server
5 Comments1 Solution523 ViewsLast Modified:
Hi,

I've got some data populated in a table and I would like to calculate the difference between row values in a particular order.

The data is like this:

uid(int),count(int)
1,5
2,7
3,15
4,10
5,12

so I would like to do a select which provides a list of differences and the second value uid:

2,2
3,8
4,-5
5,2

Is there any way to do this in SQL? I'm guessing I need to do some sort of self join, but ?

I want to do this in SQL if possible as there will be thousands of rows so extracting it all into a script and calculating it would be slow.

Thanks for you help,

Ian.
ASKER CERTIFIED SOLUTION
skullnobrains

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros