Here I use an IF
to consider each value for mark
. If the value is zero, the IF
returns a 1, otherwise 0.
These are then passed to SUM
to count how many values are zero. That sum is passed to another IF
which returns 0 for any positive number of zero values, or the SUM
of mark
otherwise. This should function even if the sequence of values changes, for example when a zero value is not first for a particular headerid
:
SELECT S.headerid as `HEADER_ID`,
IF(SUM(IF(S.`mark` = 0, 1, 0)) > 0, 0, SUM(S.`mark`)) AS `TOTAL_MARKS`
FROM sample_db.sheet_data S
GROUP BY S.`headerid`;