DROP TEMPORARY TABLE IF EXISTS tData;
CREATE TEMPORARY TABLE tData
	SELECT 1 `group`, 1 `max`, 10 `value`
	UNION SELECT 1, 3, 30
	UNION SELECT 1, 2, 20
	UNION SELECT 1, 3, 31
	UNION SELECT 1, 2, 21
	UNION SELECT 2, '4', 40
	UNION SELECT 2, '6', 60
	UNION SELECT 2, '5', 50
	UNION SELECT 2, '6', 61
	UNION SELECT 2, '5', 51
	UNION SELECT 3, 7, 70
	UNION SELECT 3, 9, 90
	UNION SELECT 3, 8, 80
	UNION SELECT 3, 9, NULL
	UNION SELECT 3, 8, 81
	UNION SELECT 4, NULL, 101
	UNION SELECT 4, NULL, 102;

DROP TEMPORARY TABLE IF EXISTS tExpect;
CREATE TEMPORARY TABLE tExpect
	SELECT 1 `group`, 31 expect
	UNION SELECT 2, 61
	UNION SELECT 3, NULL
	UNION SELECT 4, 102;

SELECT e.`group`, r.result, e.expect, r.result <=> e.expect isRight
	FROM tExpect e
		LEFT JOIN (
			SELECT `group`, multimax(`max`, `value`) result
				FROM tData GROUP BY `group`
		) r ON r.`group` = e.`group`;

DROP TEMPORARY TABLE tData, tExpect;