3
I cannot perform the percentage of this query .
You should take the sum of "valor_convenio" of each municipality and divided by the total sum of the "valor_convenio" :
<h3>Entrada SQL SEM CALCULO DE PORCENTAGEM</h3>
SELECT
nome_municipio,
SUM(valor_convenio)<br/>
FROM
paraiba.vigente<br/>
WHERE
convenente LIKE '%(MUNICIPAL)%'<br/>
GROUP BY
nome_municipio<br/>
ORDER BY
"Total Conveniado (R$)" DESC;<br/>
<h3>Exemplo de Saída ATUAL:</h3>------------------------------------------------------
<br/>|JOAO PESSOA.......|R$272.789.654,75|
<br/>|CAMPINA GRANDE|R$182.080.728,84|
<br/>|PIANCO...................|..R$35.392.580,61|
<br/>|SUME......................|..R$34.040.127,05|
<br/>|CABEDELO.............|..R$30.652.583,47|
<br/>|SOUSA....................|..R$22.075.733,70|
<br/>|PATOS.....................|..R$20.061.310,59|
<br/>-------------------------------------------------------
<h3>Exemplo de Saída QUE PRECISO:</h3>--------------------------------------------------------------
<br/>|JOAO PESSOA.......|R$272.789.654,75|..22%
<br/>|CAMPINA GRANDE|R$182.080.728,84|..15%
<br/>|PIANCO...................|..R$35.392.580,61|....3%
<br/>|SUME......................|..R$34.040.127,05|....3%
<br/>|CABEDELO.............|..R$30.652.583,47|....2%
<br/>|SOUSA....................|..R$22.075.733,70|....2%
<br/>|PATOS.....................|..R$20.061.310,59|....1%
<br/>--------------------------------------------------------------
At the end of I export to csv file separated by ";" by this pattern:
JOAO PESSOA;272.789.655;22%<br/>
CAMPINA GRANDE;182.080.729;15%<br/>
PIANCO;35.392.581;3%<br/>
SUME;34.040.127;3%<br/>
CABEDELO;30.652.583;2%<br/>
SOUSA;22.075.734;2%<br/>
PATOS;20.061.311;2%<br/>
Percentage relative to what? that 22% there in the first result is only fictitious?
– Diego Rafael Souza
I don’t know if I understood your question correctly, could you rephrase and give more details? I simulated the problem, see if it helps you: http://www.sqlfiddle.com/#! 17/b64008/5
– Caique Romero
https://stackoverflow.com/questions/8515152/how-to-use-a-sql-window-function-to-calculate-a-percentage-of-an-aggregate see if help "Analytic functions" facilitate this.
– Motta
the 22% is the total sum of the value divided by the value of the municipality in question
– Cleef Souza