• Thank you all for your help. I got the code that Frederico Fonseca. Just fixed the month in the second query and took group by . Thanks

    select t.mes

    , t.ano

    , t.mes_ano

    , t.realizadas

    , total.total_ordens_geradas

    , convert(decimal(5,2), t.realizadas / convert(decimal(5,2), total.total_ordens_geradas) * 100) as porcentagem

    from (select datepart(mm, ordserv.datpro2) mes

    , datepart(yy, ordserv.datpro2) ano

    , engeman.mesabrev(ordserv.datpro2) + '/' + substring(cast(datepart(yy, ordserv.datpro2) as varchar),3,2) mes_ano

    , sum(case when ordserv.statord='f' then 1 else 0 end) as realizadas

    from ordserv

    left outer join FILIAL on ORDSERV.CODEMP_7=FILIAL.CODEMP AND ORDSERV.CODFIL=FILIAL.CODFIL

    where ordserv.datpro2 between '01/01/2015' and '31/12/2015'

    and ordserv.statord='F'

    and ordserv.codemp=8

    and filial.codfil=25

    and 'S' in (select regserv.executado

    from regserv

    where regserv.codemp = ordserv.codemp

    and regserv.codord = ordserv.codord)

    group by datepart(mm, ordserv.datpro2)

    , engeman.mesabrev(ordserv.datpro2)

    , datepart(yyyy, ordserv.datpro2)

    ) t

    outer apply (select /*datepart(mm, ordserv.datpro2) mes

    , datepart(yy, ordserv.datpro2) ano

    , engeman.mesabrev(ordserv.datpro2) + '/' + substring(cast(datepart(yy, ordserv.datpro2) as varchar),3,2) mes_ano

    , */count(*) as total_ordens_geradas

    from ordserv

    left outer join FILIAL on ORDSERV.CODEMP_7=FILIAL.CODEMP AND ORDSERV.CODFIL=FILIAL.CODFIL

    where ordserv.datpro2 between '01/01/2015' and '31/12/2015'

    and ordserv.statord='F'

    and ordserv.codemp=8

    and filial.codfil=25

    and t.mes = datepart(mm, ordserv.datpro2)

    and t.ano = datepart(yy, ordserv.datpro2)

    /* group by datepart(mm, ordserv.datpro2)

    , engeman.mesabrev(ordserv.datpro2)

    , datepart(yyyy, ordserv.datpro2) */

    -- I do not think the call to the function is required - but up to you to see if it is or not.

    -- and t.mes_ano = engeman.mesabrev(ordserv.datpro2) + '/' + substring(cast(datepart(yy, ordserv.datpro2) as varchar),3,2)

    ) total

    order by 1,2