对于给定的日期,我想获得条纹(连续日期向后,其中数字高于/等于某个阈值 => 在示例中始终为 1000)
示例:
我将非常感谢您的帮助。非常感谢!
最简单的是使用递归:
WITH RECURSIVE streak AS (
SELECT
d.date,
d.number,
1 AS streak_counter
FROM data d
WHERE d.date = '2024-11-07'. --> Your date here
UNION ALL
SELECT
d.date,
d.number,
streak.streak_counter + 1
FROM data d
INNER JOIN streak ON d.date = streak.date - INTERVAL '1 day'
WHERE d.number >= 1000
)
SELECT MAX(streak_counter) FROM streak;