I am trying to get the highest count of the number of rows with a consecutive value. For example, if below were a table and I counted the total number of consecutive 'A's I would get 5.
- A
- B
- B
- A
- A
- A
- A
- A
- B
I am trying to find a neat way to do this in SQL. I am trying to do it with PHP but struggling, and have created a messy solution:
$streaksql = "SELECT * FROM `mytable`";
$streaksql = $modx->query($streaksql);
$outcomecounter = 0;
$highestoutcome = 0;
while ($streak = $streaksql->fetch(PDO::FETCH_ASSOC)) {
$outcome = $row['outcome'];
if($outcome == 'A'){
$outcomecounter = $outcomecounter +1;
if($outcomecounter > $highestoutcome){
$highestoutcome = $outcomecounter;
}
}
else {
$outcomecounter = 0;
}
};//WHILE
echo $highestoutcome;
I am wondering if anyone know a neater way to do this in an SQL query?