LST LST - 1 month ago 15
MySQL Question

MySQL group calculation ignoring 0 status group

I want to get sum of a column of a group (marks), according to another column (is_goodDegree) only contains value 1. and with group by san and temp_key

Check below image;

enter image description here

It should calculate group 2 marks (45+43+47) while it should ignore Group 1.

How can I write a SQL query for this? please advise.

Answer

Use the HAVING clause :

SELECT t.san,SUM(t.marks) as sum_marks
FROM YourTable t
GROUP BY t.san
HAVING MIN(t.is_goodDegree) = 1
Comments