Skip to content
Advertisement

Why did I sum the wrong result?

I’d like to have an advice for this SUM that doesn’t work as expected. I have to SUM data from the same day and display it in a table. I did a query like this:

SELECT * FROM giorni WHERE punto = '$punto' && STR_TO_DATE(giorno, '%d/%m/%Y') BETWEEN   STR_TO_DATE('" . $d1 . "', '%d/%m/%Y') AND STR_TO_DATE('" . $d2 . "', '%d/%m/%Y') ORDER BY data";

This is the ‘simple’ one and it displays every single row, I have to put a SUM in it to make it show every day but SUM data from the same day.

These 2 rows must be shown as one: enter image description here

Can anyone give me any help to create this query?

Advertisement

Answer

if I understand correctly you can try this.

You can’t put multiparameter in SUM function.

When you use Aggregate function you need to add non-Aggregate columns inGROUP BY caluse.

 SELECT data,giorno,SUM(a1) as a1, SUM(a2) as a2, SUM(a3) as a3 , SUM(a4) as a4 
 FROM giorni 
 WHERE punto = '$punto' && STR_TO_DATE(giorno, '%d/%m/%Y') 
 BETWEEN STR_TO_DATE('" . $d1 . "', '%d/%m/%Y') AND STR_TO_DATE('" . $d2 . "', '%d/%m/%Y') 
 GROUP BY data,giorno
 ORDER BY data";
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement