Sum PL SQL Lines

Asked

Viewed 186 times

1

Galerinha, I created a code that organizes all the tasks of the company by sector. The objective of this code is to identify the percentage of delayed activities by sector. The result was expected.

IMG1

Now I have gathered and told the different tasks. IMG 2

I wanted to know how to calculate the percentage for each Sector. I tried what I remembered, but I’m out of ideas.

It would be the sum total of the columns my 100% And for sectors would be total sector / total columns.


I tried something like:

select 


sum(COUNT(DISTINCTTASK_SEQ)),
SUM(SETOR = 'CALDEIRARIA')/(SUM(CONTAGEM)) AS CALDEIRARIA,
SUM(SETOR = 'ELETRICA')/(SUM(CONTAGEM)) AS ELETRICA,
SUM(SETOR = 'INSPECAO')/(SUM(CONTAGEM)) AS INSPECAO,
SUM(SETOR = 'INSTRUMENTACAO')/(SUM(CONTAGEM)) AS INSTRUMENTACAO,
SUM(SETOR = 'MECANICA')/(SUM(CONTAGEM)) AS MECANICA,
SUM(SETOR = 'OPERACAO')/(SUM(CONTAGEM)) AS OPERACAO,
SUM(SETOR = 'OUTROS')/(SUM(CONTAGEM)) AS OUTROS,

from petro_backlog_equipestotal
group by SETOR

Thanks in advance.

  • Read about Analytic functions, mainly the clause OVER. https://www.orafaq.com/node/55

1 answer

1


The RATIO_TO_REPORT function is native to Oracle, and aims to show the proportion of a given quantity in Relation to Total.

SELECT NOME_SETOR, 
       ROUND( ( (RATIO_TO_REPORT(QTDE) OVER())*100 ) ,2 ) 
  FROM (
          SELECT NOME_SETOR SETOR, COUNT(*) QTDE    
            FROM TAREFAS 
          GROUP BY NOME_SETOR
       )
  • This one I didn’t know !

  • Opa! but I do not have columns for each sector. Ta all in two columns only! It is in them that I need to perform the operation. The code I tried above doesn’t work.

  • I need to see your full select line.

  • I sought more information about said command and the way you reported, it was thanks to that I got. 100% useful, thank you. The final code looks like this: select

EQUIPE,
COUNT(TAREFA) AS TAREFA,
ROUND(RATIO_TO_REPORT(COUNT(*)) OVER()*100, 2) AS PORCENTAGEM

from petro_backlog_equipestotal
GROUP BY EQUIPE;

Browser other questions tagged

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