1
I need to add some columns in Mysql, use the sum to make an average and make the columns available for display in a report, I tried to make the sum in the query in Mysql but it did not work, I will try to explain better with a picture of my database, I tried to make the sum using SUM
thus:
The sql
that picture is like this:
SELECT af.idAfericao, af.data, af.produto, af.deterUmidade, af.deterVal1, af.deterVal2, af.deterVal3, af.deterVal4, af.deterVal5, SUM(af.deterValMedia) AS SomaVarMedia, af.modeloDestilador, SUM(af.resultadoDestilacao) AS SomaDestilacao, SUM(af.diferenca) AS SomaDiferenca, af.ativo, cu.descricao, cc.nome FROM comUnidade cu, afericaoAgricola af, comColaborador cc WHERE af.idUnidade = 6 AND af.idUnidade = cu.idUnidade AND af.ativo = 1 AND af.classificador = cc.idColaborador AND af.data BETWEEN '2016-02-10' AND '2016-02-16' AND af.deterUmidade='Dickey-John' AND af.produto='Soja' AND af.modeloDestilador='CA 50' GROUP BY af.idAfericao ORDER BY af.data DESC
I wish I could have the result of the columns to display in my report, but I also need the sum of each column to make an average.
Do a subquery on the select line only to calculate the sum and average meets you?
– lpacheco
Do you want the sum to be divided into each line? That is, so that this Sum field is displayed only in the last line of the report in the totals?
– gabrieloliveira
Hello @Ipacheco, thanks for the reply.
– adventistapr
Hello @gabrieloliveira, I need a sum of the columns exactly as you said.
– adventistapr
What software are you using for reporting? I may be mistaken but in terms of query you would have no way to bring a line different from the others, they contain the same fields. In this case you need a line of totals, which is usually done in the application that handles the data. You only wanted this result with a Mysql query?
– gabrieloliveira
I’m using Dreamweaver CS6 @gabrieloliveira, you mean it would be better to do the sum by Dreamweaver?
– adventistapr
But will this report be available in some web application? Or do you only need to generate it once?
– gabrieloliveira
It is generated whenever the user needs @gabrieloliveira.
– adventistapr
So, but are you building an application to read this data and generate the report? In PHP?
– gabrieloliveira
If you want a line with SUM only at the end, use UNION to join the list SELECT with the SUM SELECT. Use the same conditions in the WHERE of both.
– Bacco