select id, num, lead(num, 1) over (order by id) as lead_num, lag(num, 1) over (order by id) as lag_num lead(num, 1) over (order by id) as lead_num, lag(num, 1) over (order by id) as lag_num This part ...
LAG(num, 1) OVER (ORDER BY id) AS prev1, LAG(num, 2) OVER (ORDER BY id) AS prev2 JOIN Logs l2 ON l1.id = l2.id - 1 JOIN Logs l3 ON l2.id = l3.id - 1 ...