Hi @YUI-012 , Welcome to Microsoft Q&A,
You seem to be using MySQL database. Your problem is mainly to find out how many latest consecutive 0.
string query = @"
SELECT COUNT(*) AS ZeroCount
FROM (
SELECT value1,
@isZero := IF(value1 = 0 AND @isZero = 1, 1, 0) AS isZeroFlag
FROM your_table, (SELECT @isZero := 1) AS init
ORDER BY id DESC
) AS consecutiveZeros
WHERE isZeroFlag = 1;
";
@isZero: This is a MySQL session variable. We initialize @isZero to 1 (assuming it is 0 at the beginning), then iterate over each row, if value1 is 0 and the previous one is also 0, isZero remains 1, otherwise it is set to 0. Sorting: Sort the records by id DESC, starting with the latest record. Filtering: Finally, we filter the rows with isZeroFlag = 1, indicating that they belong to the continuous 0 starting from the latest one.
Then you can judge the result.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.