Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

mysql query : SUM function


Pica

Recommended Posts

I created a new table in osC and added some data to it.

 

It all works great except that I am unable to do a SUM with the values in that table.

 

I can query and display various informations, do COUNT, etc...

 

Here is what I need to do :

 

Table structure :

 

Index - User_id - Month - Quantity
1 - 1 - june - 35
2 - 2 - june - 17
3 - 3 - june - 45
4 - 1 - july - 23
5 - 2 - july - 38
6 - 3 - july - 13
...

 

Presently, I used the following code :

 

$comm_query_quantity = tep_db_query("select Quantity from " . TABLE_SALES . " where (User_id = '" . $customer_id . "' and Month = 'june')");

$comm_quantity = tep_db_fetch_array($comm_query_quantity);

 

In my page code I used this in order to display the info :

 

...
<span class="copy" align="middle">' . $comm_quantity['Quantity'] . '</span>
...

 

Let's say that User 1 is logged in... I get the result : 35

That's fine.

 

What I need to do now is to have the SUM of ALL Users for the month june which should be 97 (35 + 17 + 45).

 

Once I get that value, I can have a percentage like 36.02 % (35 / 97).

 

...
<span class="copy" align="middle">' . round(($comm_quantity['Quantity'] / $comm_quantity_sum['???'] * 100), 2) . ' %</span>
...

 

So, I need to know how to determine $comm_quantity_sum['???'] !

 

Does this make any sense to you ?

 

Thanks a 1.000.000 in advance for your help.

 

;o)

 

Pica.

Link to comment
Share on other sites

Well... I was so drown in the code that I probably did some stupid thing....

 

Seeing I changed and changed while trying... I can't say what I had there for sure.

 

I just got it to work...

 

SELECT SUM(quantity) AS total_quantity FROM " . TABLE_SALES . " where month = 'june'

 

then

 

<span class="copy" align="middle">' . round(($comm_quantity['quantity'] / $comm_quanity_t['total_quantity'] * 100), 2) . ' %</span>

 

and I get my %

 

But I guess this is not new to you, tight ?

 

Thanks anyway for your reply.

 

:rolleyes:

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...