Navi
Navi

Reputation: 8736

Subtracting sequential values with MySql stored procedure

Starting with an ordered set of floating point values (the order matters) that can be retrieved with a simple query. I want to calculate sequential differences of values with a stored procedure. For instance,

if we have the values

1, 3, 7

the result should be

2, 4

What is the easiest way to do this with a stored procedure (Mysql 5)?

Upvotes: 1

Views: 1082

Answers (1)

Doug Kress
Doug Kress

Reputation: 3537

Something like this might work:

CREATE TEMPORARY TABLE x (seq int);
INSERT INTO x VALUES (1), (3), (7);
SET @dif = 0;
SELECT seq - @dif, @dif:=seq FROM x ORDER BY seq;

Upvotes: 3

Related Questions