Page 1 of 1

How to get the sum of the column?

Posted: Tue Dec 29, 2009 10:52 am
by MicroBoy
How to get the sum of the column, for example like in the photo?

Image

Re: How to get the sum of the column?

Posted: Tue Dec 29, 2009 1:27 pm
by manohoo
When I learned Oracle's SQL there was a REPORT feature that allowed you to do what you want. However, in MySQL the only way that I know how is to run 2 separate queries. In your case it would be something like this:

Code: Select all

$query1 = "SELECT Muaji, Fitimi, Bortxh, Fitimi_Total from TABLE";
$query2 = "SELECT Sum(Fitimi_Total) from TABLE ";
Then do your usual HTML/PHP to bring'em together into a table.

Re: How to get the sum of the column?

Posted: Tue Dec 29, 2009 1:45 pm
by bytebrite
Assuming mysql and that you want the sum of last column only, you can do it in one query:

Code: Select all

select label, b, c, d, sum(b + c + d) from scratch group by label with rollup

Re: How to get the sum of the column?

Posted: Tue Dec 29, 2009 6:50 pm
by MicroBoy
manohoo wrote:When I learned Oracle's SQL there was a REPORT feature that allowed you to do what you want. However, in MySQL the only way that I know how is to run 2 separate queries. In your case it would be something like this:

Code: Select all

$query1 = "SELECT Muaji, Fitimi, Bortxh, Fitimi_Total from TABLE";
$query2 = "SELECT Sum(Fitimi_Total) from TABLE ";
Then do your usual HTML/PHP to bring'em together into a table.
I think that there is a problem to do this, Because to show this data in the table, I use the above code. To be more clear, I have all the sales in a table in MySQL, then Here I collect all the sales from "November - 2009", "December 2009" etc... So Now I want to calculate all the sales of 2009, all the sales that are in the Table(in photo).

The Code:

Code: Select all

$sql = mysql_query("SELECT SUM(qtotal), SUM(borxh), DATE_FORMAT(data, '%m/%Y') AS dataere FROM riparimet GROUP BY dataere ORDER BY data DESC");
p.s. In the photo, the row where it's "Nëntor - 2008" it's wrong, cause really it's "Gusht - 2009"

Re: How to get the sum of the column?

Posted: Tue Dec 29, 2009 7:02 pm
by bytebrite

Re: How to get the sum of the column?

Posted: Tue Dec 29, 2009 7:08 pm
by Manny7
just

Code: Select all

 
$sql = mysql_query("SELECT Sum(Fitimi_Total) from TABLE");
 
don't go ???

Re: How to get the sum of the column?

Posted: Wed Dec 30, 2009 7:45 am
by MicroBoy
Thanks a lot for help, I made another query too, and did not group data in the other query. Because in the first query I grouped them.

Re: How to get the sum of the column?

Posted: Wed Dec 30, 2009 8:00 am
by manojsemwal1
do one thing take the $Fitimi_Total value in loop and add like

before loop define $i=0;
inside the Loop $Fitimi_Tetal=$Fitimi_Tetal+$i;

and the Last just print Total $Fitimi_Tetal