SELECT DISTINCT M.CODCOLIGADA AS COLIGADA,M.CODDISC AS CODDISC,DBO.TIRAACENTOS(M.DISCIPLINA) AS DISCIPLINA,PE.CODUSUARIO ,C.NOME AS CURSO,M.CODPERIODO
                ,M.IDHABILITACAOFILIAL
                FROM SHISTDISCPENDENTES M
                INNER JOIN CORPORERM.DBO.SALUNO S ON
                M.CODCOLIGADA = S.CODCOLIGADA AND
                M.RA = S.RA
                INNER JOIN CORPORERM.DBO.SHABILITACAOFILIAL SH ON
                SH.CODCOLIGADA = M.CODCOLIGADA AND
                SH.IDHABILITACAOFILIAL = M.IDHABILITACAOFILIAL
                INNER JOIN CORPORERM.DBO.SCURSO C ON
                C.CODCOLIGADA = SH.CODCOLIGADA
                AND C.CODCURSO = SH.CODCURSO
                INNER JOIN CORPORERM.DBO.PPESSOA PE ON
                S.CODPESSOA = PE.CODIGO
                INNER JOIN (SELECT H.CODCOLIGADA,H.CODSTATUS,H.IDHABILITACAOFILIAL,M.RA,MAX(M.PERIODO) PERIODO,max(IDPERLET) IDPERLET,1 as Qtde FROM SHABILITACAOALUNO H
                INNER JOIN SMATRICPL M ON H.CODCOLIGADA = M.CODCOLIGADA AND H.RA = M.RA AND H.IDHABILITACAOFILIAL = M.IDHABILITACAOFILIAL
                WHERE H.CODSTATUS NOT IN (01,10,13,19,4,8,9)
                GROUP BY H.CODCOLIGADA,H.CODSTATUS, H.IDHABILITACAOFILIAL,M.RA) G
                ON G.CODCOLIGADA = M.CODCOLIGADA AND G.RA = M.RA AND G.IDHABILITACAOFILIAL = M.IDHABILITACAOFILIAL
                WHERE ISNULL(M.STATUS,'') NOT LIKE '*Equiv.' AND ISNULL(M.STATUS,'') NOT LIKE 'MATRICULADO' AND ISNULL(M.STATUS,'') NOT LIKE 'PRÉ-MATRÍCULA' AND M.CODPERIODO <= G.PERIODO
                AND M.DISCIPLINA NOT LIKE '%ATIVIDADES COMPLEMENTARES%' AND M.DISCIPLINA NOT LIKE '%ATIVIDADES FORMATIVAS%'
                AND PE.CODUSUARIO = :codusuario
                AND CORPORERM.DBO.TIRAACENTOS(C.NOME) = :curso
                ORDER BY C.NOME