SELECT * from (
   SELECT 
		c.name AS curso,
		cl.num_enterprise AS Empresa,
		cl.tip_colaborador AS tipoColaborador,
		concat(u.first_name, u.last_name) AS Colaborador,
		cl.mat_colaborador AS Matricula,
		(
			CASE
				WHEN cl.tip_colaborador = 1 && cl.num_enterprise = 1 THEN 'Empregado'
				WHEN cl.tip_colaborador = 1 && cl.num_enterprise = 3 THEN 'Cooperado'
				WHEN cl.tip_colaborador = 1 && cl.num_enterprise = 3000 THEN 'Empregado'
				ELSE 'Terceiro'
			END
		
		) AS TipoVinculo,
        ma.name AS Gestao,
		s.name AS Setor,
		jr.name AS Cargo,
		IF(u.enabled = 1, 'Ativo', 'Ferias/Demitido/Afastado') AS Situacao,
        ue.dt_update AS data,
		COUNT(ue.id) AS Tentativas,
        MAX(ue.grade) AS nota_atingida,
        e.minimum_grade AS nota_minima
    FROM user_evaluation ue
    INNER JOIN evaluation e 
        ON ue.evaluation_id = e.id
    INNER JOIN module m 
    	ON e.module_id = m.id
    INNER JOIN course c 
    	ON m.course_id = c.id
    INNER JOIN user u 
    	ON ue.user_id = u.id
    INNER JOIN client cl 
    	ON u.id = cl.id
    INNER JOIN sector s 
    	ON u.sector_id = s.id
    INNER JOIN management ma 
    	ON u.management_id = ma.id
    INNER JOIN job_role jr 
    	ON u.job_role_id = jr.id
    GROUP BY ue.user_id, ue.evaluation_id
    ORDER BY u.first_name, c.name
) AS tbl
WHERE nota_atingida < nota_minima