Consecutive Numbers

You can achieve this result by using the LEAD and LAG window functions to compare the current row with the previous and next rows. The following query demonstrates how to do this:

1
2
3
4
5
6
7
8
SELECT DISTINCT l1.num AS ConsecutiveNums
FROM (
  SELECT num,
         LAG(num, 1) OVER (ORDER BY id) AS prev_num1,
         LAG(num, 2) OVER (ORDER BY id) AS prev_num2
  FROM Logs
) AS l1
WHERE l1.num = l1.prev_num1 AND l1.num = l1.prev_num2;

This query first creates a derived table with the LEAD and LAG functions to get the values of the previous two rows. Then, it filters the derived table to rows where the current row, previous row, and next-to-previous row all have the same value, and returns the distinct values that meet this criteria.