Calculating the difference between subsequent values in SQL

Posted in MySQL by - July 12, 2024

It’s sometimes desirable to caculate the difference between subsequent points.

This might be necessary, for example, if a record’s value is drawn from a cumulative total (such as packet counter on a switch) – we’re not really interested in the total value so much as the change over time.

The LAG function can be used to look at the previous value, whilst LEAD can be used to look at the next

If the source is something like a packet counter, it might also be desirable to generate a non-negative difference so that a counter reset doesn’t lead to large negative figures being reported. Continue Reading

This post was written by

Leave a Reply

Your email address will not be published. Required fields are marked *

Please enter and activate your license key for Cryptocurrency Widgets PRO plugin for unrestricted and full access of all premium features.