Task: it is necessary to show how many boys and girls received this or that assessment.
A "clumsy" request was written for the task:
SELECT r.mark, COUNT(r.mark), (SELECT COUNT(r1.mark) FROM results r1 JOIN users u1 ON (r1.id_user = u1.id AND r1.ex_mode = 'tmOlympDiagnostic' AND u1.gender = 'Ж')) FROM results r JOIN users u ON (r.id_user = u.id AND r.ex_mode = 'tmOlympDiagnostic' AND u.gender = 'М') GROUP BY r.mark ORDER BY r.mark desc; Conclusion:
Mark | BoysMarksCount | GirlsMarksCount ---------+----------------+---------------- 5 | 8 | 2 4 | 1 | 2 3 | 1 | 2 2 | 2 | 2 1 | 1 | 2 ---------+----------------+---------------- For boys, it was not difficult to do statistics, but for girls I don’t know how to issue a request correctly, so that the third column shows how many of each assessment girls received, because now the third column is not grouped by the "Mark" field and as a result, having diagnostic results only from two girls, in each column displays 2 pcs. for each evaluation.