我有下面的Logs数据集表格,其中"ID"列是身份,"Num"列代表一个简单的数字,可以有重复项.我只想输出"Num"列中连续出现两次以上的值.例如:值1连续出现三次,因此前3行的输出结果为1.但是,值1和值2都没有连续重复超过2次,因此它们不在输出中:
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
Output:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
我try 了下面的查询,但它四次返回数字1,并且无法检测到输出所需的"连续"性质:
with cte as(
select logno,grp = sum(case when logno = repeat_val then 0 else 1 end) over (order by sno) from
(
select sno,logno,
lag(logno) over(order by logno) as repeat_val
from
logprac l) s)
select logno as ConsecutiveNums , count(*)
from cte
group by grp,logno
having count(*)>2
order by grp;