SELECT * FROM (

    # RECUPERA CURSOS QUE NÃO POSSUEM PROVA
    SELECT 
        c.pontuation AS 'Participação Pontuada',
        REPLACE(c.name,',','') AS Curso,
        concat(u.first_name, u.last_name) AS Nome,
        cl.mat_colaborador AS 'CRM',
        u.email AS 'E-mail',	
		'Cooperado' AS 'TipoVinculo',
        REPLACE(jr.name,',','') AS Especialidade,
		IF(u.enabled = 1, 'Ativo', 'Ferias/Demitido/Afastado') AS Situacao,
        ue.dt_update AS DataConclusao,
        MAX(ue.grade) AS Nota,
        IF(MAX(ue.grade) >= 70,'Aprovado','Reprovado') AS 'Status' # CURSO SEM PROVA A NOTA É SEMPRE 100% E O STATUS APROVADO
    FROM user_evaluation ue
    INNER JOIN course c 
        ON ue.course_id = c.id
    INNER JOIN user u 
        ON ue.user_id = u.id
    INNER JOIN client cl 
        ON cl.id = u.id
    LEFT JOIN management ma 
        ON u.management_id = ma.id
    LEFT JOIN sector s 
        ON u.sector_id = s.id
    LEFT JOIN job_role jr 
        ON u.job_role_id = jr.id
    WHERE 
        cl.num_enterprise  = 3 AND
		cl.tip_colaborador = 1 AND
        ue.evaluation_id IS NULL AND
        c.pontuation > 0 AND
		u.enabled = 1 AND
		c.active = 1
    GROUP BY ue.course_id, ue.evaluation_id, ue.user_id

    UNION ALL

    # RECUPERA CURSOS QUE POSSUEM PROVA
    SELECT
        c.pontuation AS 'Participação Pontuada',
        REPLACE(c.name,',','') AS Curso,
        concat(u.first_name, u.last_name) AS Nome,
        cl.mat_colaborador AS 'CRM',
        u.email AS 'E-mail',	
		'Cooperado' AS 'TipoVinculo',
        REPLACE(jr.name,',','') AS Especialidade,
		IF(u.enabled = 1, 'Ativo', 'Ferias/Demitido/Afastado') AS Situacao,
        ue.dt_update AS DataConclusao,
        MAX(ue.grade) AS Nota,
        IF(MAX(ue.grade) >= e.minimum_grade,'Aprovado','Reprovado') AS 'Status' # CURSO SEM PROVA A NOTA É SEMPRE 100% E O STATUS APROVADO
    FROM user_evaluation ue
    INNER JOIN evaluation e 
        ON ue.evaluation_id = e.id
    INNER JOIN user u 
        ON ue.user_id = u.id
    INNER JOIN client cl 
        ON cl.id = u.id
    LEFT JOIN sector s 
        ON u.sector_id = s.id
    LEFT JOIN management ma 
        ON u.management_id = ma.id
    LEFT JOIN job_role jr 
        On u.job_role_id = jr.id
    LEFT JOIN module m 
        ON e.module_id = m.id
    LEFT JOIN course c 
        ON m.course_id = c.id
    WHERE 
		cl.num_enterprise  = 3 AND
		cl.tip_colaborador = 1 AND
        ue.evaluation_id IS NOT NULL AND
        c.pontuation > 0 AND
		u.enabled = 1 AND
		c.active = 1
    GROUP BY ue.user_id, ue.evaluation_id, ue.course_id
    
) AS tbl WHERE Status = 'Aprovado'