I have recently been developing a query in which I had the need to compare successively the rows of the same data set , I mean, from the same column, from the same table. In this, I became aware of the MySQL LEAD () and LAG () functions. However, I later verified that these are "window functions" functions and may not be available for MySQL. Ask the question: Can you simulate your operation?
MySQL and the Window Functions
Discussion about of "Window Functions" and their unavailability in MySQL.
SQLFiddle (The query I've been developing):
Query search to return in distinct columns, based on your value interval between them. To do this "disregard" the date provided by the timestamp and converting the period before in HH: MM: SS format to only hours.
Simulate LAG and LEAD Function:
I found a similar post in StackOverflow and Database Administrator, though I checked that there was nothing about it here, and I was not able to apply the answer to my case. Dai saw the need to open a question.
LEAD Function - MySQL (Summary on aa LEAD function)
The LEAD () function is a window function that allows you to search a number of rows and access data from that row from the current row. Similar to the LAG () function, the LEAD () function is very useful for calculating the difference between the current row and the subsequent row within the same result set.
Example:
mysql> SELECT
t, val,
LEAD(val) OVER w AS 'lead',
val - LEAD(val) OVER w AS 'lead diff'
FROM series
WINDOW w AS (ORDER BY t);
+----------+------+------+--------- -+
| t | val | lead | lead diff |
+----------+------+------+-----------+
| 12:00:00 | 100 | 125 | -25 |
| 13:00:00 | 125 | 132 | -7 |
| 14:00:00 | 132 | 145 | -13 |
| 15:00:00 | 145 | 140 | 5 |
| 16:00:00 | 140 | 150 | -10 |
| 17:00:00 | 150 | 200 | -50 |
| 18:00:00 | 200 | NULL | NULL |
+----------+------+------+-----------+
EDITION 1:
I hope to achieve a similar result:
+----------+----------+---------------+----------+----------+---------------+
| t p/ v=1 | lead v=1 | lead diff v=1 | t p/ v=0 | lead v=0 | lead diff v=0 |
+----------+----------+---------------+----------+----------+---------------+
| 20.8806 | (null) | (null) | (null) | (null) | (null) |
| 20.8764 | 20.8806 | -0,0042 | (null) | (null) | (null) |
| 20.87 | 20.8764 | -0,0064 | (null) | (null) | (null) |
| 20.8636 | 20.87 | -0,0064 | (null) | (null) | (null) |
| 20.8508 | 20.8636 | -0,0128 | (null) | (null) | (null) |
| 20.85 | 20.8508 | -0,0008 | (null) | (null) | (null) |
| (null) | (null) | (null) | 20.8333 | (null) | (null) |
| (null) | (null) | (null) | 20.8303 | 20.8333 | -0,003 |
| (null) | (null) | (null) | 20.83 | 20.8303 | -0,003 |
| (null) | (null) | (null) | 20.8 | 20.83 | -0,03 |
| (null) | (null) | (null) | 20.5 | 20.8 | -0,3 |
| (null) | (null) | (null) | 20.4964 | 20.5 | -0,0036 |
+----------+-----------+--------------+----------+----------+---------------+
- The column lead v = n moves a line in reference to the column to its left tp / v = n The lead diff v = n column calculates the difference between the other two columns