我目前有这组数据。
<表类>
Id
模式
tbody><<tr>1 1 22 3 1 42 53 64 71 82 9 1 102 113 表类>
使用SUM()
窗口函数创建序列组,并使用MAX()
窗口函数检查每组中的最大模式:
SELECT Id, pattern
FROM (
SELECT *, MAX(pattern) OVER (PARTITION BY grp) max_pattern
FROM (
SELECT *, SUM(pattern = 1) OVER (ORDER BY Id) grp
FROM tablename
) t
) t
WHERE max_pattern >= 3;
或者,对于不支持窗口函数的MySql版本,使用相关子查询:
SELECT t1.*
FROM tablename t1
WHERE (SELECT t2.pattern FROM tablename t2 WHERE t2.Id = t1.Id + (3 - t1.pattern)) = 3;