# [180. Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers/description/?envType=study-plan-v2&envId=top-sql-50) ![image](https://hackmd.io/_uploads/HJRmxZy06.png) ![image](https://hackmd.io/_uploads/SyENgbkAp.png) WITH CTE 一般資料表運算式(Common Table Expression) LEAD() 函數是一個視窗函數,可讓您向前看多行並從當前行存取行的數據 所以我們先建一個CTE表 由於題目要連續3個同樣的數 所以我們使用lead平移1,2 ``` with cte as ( select num, lead(num,1) over() num1, lead(num,2) over() num2 from logs ) ``` 平移之後 我們將CTE表放入select num=num1 and num2 代表連續3個 ```SQL with cte as ( select num, lead(num,1) over() num1, lead(num,2) over() num2 from logs ) select distinct num AS ConsecutiveNums from cte where (num=num1) and (num=num2) ```