public
Authored by
PotatoGim

[SQL] take diff between two rows based on timestamp
SELECT
curr.scope,
curr.name,
next.scope,
next.name,
from_unixtime(curr.time) AS curr_time,
from_unixtime(next.time) as next_time,
IF(ISNULL(next.time), 0, next.time - curr.time) AS d_t,
curr.fs_write AS curr_writes,
next.fs_write AS next_writes,
IF(ISNULL(next.fs_write), 0, (next.fs_write - curr.fs_write) / (next.time - curr.time)) AS d_bytes
FROM
fs_stats AS curr
LEFT OUTER JOIN
fs_stats AS next
ON
next.time = (
SELECT MIN(time)
FROM fs_stats
WHERE scope = 'VM5SET4-1' AND name like '%preset_vol%' AND time > curr.time)
AND next.scope = 'VM5SET4-1'
AND next.name like '%preset_vol%'
WHERE curr.scope = 'VM5SET4-1' AND curr.name like '%preset_vol%'
ORDER BY curr.time;
Please register or sign in to comment