如何select一个数据在mysql连续3个月以上bonus > 0?

How to select a data in mysql with 3 or more consecutive months and bonus > 0?

我目前正在尝试select一个agentID连续3个月或以上有bonus的数据

这是我目前的进度。

if($stmt = $conn->query("SELECT agentID, bonus FROM sample_tbl where agentID = '61599' && bonus > 0")){

    $elligible = $stmt->num_rows;

    if($elligible >= 3){
        echo "You have received a bonus for 3 or more consecutive months!";
    }else{
        echo "You have not received a bonus for 3 or more consecutive months!";
    }
  }else{
  echo $connection->error;
  }

我的代码有效!但我想添加一个可以检测月份的过滤过程。

这是我 table 的照片。

如果我使用 agentID 61599,它应该说“我没有收到奖金...”,因为它在“2022”年“1”月的奖金为 0。它不再与月份连续或按顺序排列。

我尝试查找查询操作方法,但失败了。有知道怎么做的请留言,谢谢

WITH cte AS (
    SELECT agentID, 
           SUM(bonus > 0) OVER (PARTITION BY agentID 
                                ORDER BY `year` * 12 + `month`
                                RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) flag
    FROM test
)
SELECT agentID
FROM cte
WHERE flag = 3;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ecbc15f6f2ac886adcde6eeee61925a7