Percentage in SQL

Asked

Viewed 130 times

0

I have the following consultation code:

SELECT
    AC.DT_AGENDA               AS DATA,
    ME.DS_MULTI_EMPRESA        AS EMPRESA,
    AC.CD_UNIDADE_ATENDIMENTO  AS CD_UNID_ATENDIMENTO,
    UA.DS_UNIDADE_ATENDIMENTO  AS UNIDADE_ATENDIMENTO, 
    COUNT(IAC.NM_PACIENTE)     AS TOTAL_MARCADOS,
    COUNT(IAC.CD_ATENDIMENTO)  AS ATENDIDOS

FROM AGENDA_CENTRAL AC
    INNER JOIN IT_AGENDA_CENTRAL   IAC   ON AC.CD_AGENDA_CENTRAL      = IAC.CD_AGENDA_CENTRAL
    INNER JOIN UNIDADE_ATENDIMENTO UA    ON UA.CD_UNIDADE_ATENDIMENTO = AC.CD_UNIDADE_ATENDIMENTO
    INNER JOIN MULTI_EMPRESAS      ME    ON ME.CD_MULTI_EMPRESA       = AC.CD_MULTI_EMPRESA
WHERE IAC.NM_PACIENTE IS NOT NULL
GROUP BY AC.CD_UNIDADE_ATENDIMENTO,
    UA.DS_UNIDADE_ATENDIMENTO,
    ME.DS_MULTI_EMPRESA,
    AC.DT_AGENDA  

I want to know based on count, how many percent represents the ATENDIDOS about count TOTAL_MARCADOS.

That is to say,

10 marked 5 Attended 50% were met from the total marked.

1 answer

1


Try,

SELECT
    AC.DT_AGENDA               AS DATA,
    ME.DS_MULTI_EMPRESA        AS EMPRESA,
    AC.CD_UNIDADE_ATENDIMENTO  AS CD_UNID_ATENDIMENTO,
    UA.DS_UNIDADE_ATENDIMENTO  AS UNIDADE_ATENDIMENTO, 
    COUNT(IAC.NM_PACIENTE)     AS TOTAL_MARCADOS,
    COUNT(IAC.CD_ATENDIMENTO)  AS ATENDIDOS,
    COUNT(IAC.CD_ATENDIMENTO) / COUNT(IAC.NM_PACIENTE) * 100 AS PERCENTAGEM_ATENDIDOS

FROM AGENDA_CENTRAL AC
    INNER JOIN IT_AGENDA_CENTRAL   IAC   ON AC.CD_AGENDA_CENTRAL      = IAC.CD_AGENDA_CENTRAL
    INNER JOIN UNIDADE_ATENDIMENTO UA    ON UA.CD_UNIDADE_ATENDIMENTO = AC.CD_UNIDADE_ATENDIMENTO
    INNER JOIN MULTI_EMPRESAS      ME    ON ME.CD_MULTI_EMPRESA       = AC.CD_MULTI_EMPRESA
WHERE IAC.NM_PACIENTE IS NOT NULL
GROUP BY AC.CD_UNIDADE_ATENDIMENTO,
    UA.DS_UNIDADE_ATENDIMENTO,
    ME.DS_MULTI_EMPRESA,
    AC.DT_AGENDA  
  • 1

    Didn’t roll, this float Convert works for oracle? Gives Error Ora936 Expression not found, I thought it was just a ')' that was missing to close the expression, but not.

  • 1

    You’re right. See the edit. Removing CONVERT should get the desired result.

  • That’s exactly what I did and it worked. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.