Jump to content

summing values in array


mrscoop

Recommended Posts

Hello,

Am working on a custom joomla component....

 

I want to sum db column TICS where column TACS equals 0 and add it to sum column TACS

The query needs to be grouped by a separate column name TOE.

 

TICS           TACS

5                    0

5                    5

1                    0

3                    3

 

so the answer should be

(5 + 1) + (5+ 3)=14

 

I stumble over arrays and formating queries to make certain types of arrays.  I figure 2 queries would be silly.

 

If anyone could point me in the right direction regarding this query I'd be much obliged. Thanks so much for taking the time to read my question!

Edited by mrscoop
Link to comment
Share on other sites

Sorry, too late to edit so here is a revision

 

TICS           TACS     TOE

5                    0          a

3                    5          c

1                    0          d

9                    3          c

 

should come to 14:

 

(5 + 1) + (5+ 3)=14

Link to comment
Share on other sites

Thanks for the information, it has definitely headed me in the right direction:

I expected the sum of TACS to be 9 as the query is grouped by TOE
But the sum is 18

$query = $this->_db->getQuery ( true );
$query->select( 'sum(if(tacs=0, tics, tacs))');
$query->from ( 'table ');
$query->group('toe');
$this->_db->setQuery ( $query );
$testsums = $this->_db->loadRowList();

 

returns this array:  array {10,18,12)

Here is a new table with my actual values
TICS        TACS        TOE

6                 9          a
12               0          c
10               0          d
4                 9          a

Expected results: 9 (grouped tacs) 10 + 12  (tics where tacs is zero)

 

I appreciate your help.  I am inspired to really learn instead of just getting by!

Link to comment
Share on other sites

I expected the sum of TACS to be 9 as the query is grouped by TOE

 

Um, no. You are grouping the two records

 

TICS        TACS        TOE

---------------------------------------

6                 9          a

4                 9          a

 

And, in the query you using a GROUP BY and a SUM() function. The SUM() function will SUM the grouped records. 9 + 9 = 18.

Link to comment
Share on other sites

Thanks Psycho, Now you further see why I need help!  How do I accomplish the goal? 

 

I have no clue, since you were supplied a solution for what you asked. You keep changing the problem, just look at the sample data you posted in your first post, third post and fifth post. In this last go around you state you expect the TACS result for the records with a TOE of a to be '9' when you do a GROUP BY. Well, your last example data had:

 

TICS        TACS        TOE

---------------------------------------

6                 9          a

4                 9          a

 

So, both records have a 9. So, there is no way for us to have any clue as to what logic to provide you. What if they were different? Do you want the highest value, the lowest, an average, or what? If is very frustrating to provide free help to people to provide a solution only to have them change the requirements. So, have some consideration and clearly explain what it is you are trying to achieve. If this TIC TAC TOE nonsense is just that, then explain what you are really doing as it may add some context to help us understand better.

Link to comment
Share on other sites

In my original question I assumed that there would be a single query and I would work with the array. Because I am a beginner!  When I followed requnix's advice I did not get the desired results.  So I posted new data.

 

If my question irritates you you do not need to respond. It is not helpful!

Link to comment
Share on other sites

So's y'all know, this chart:

 

TICS        TACS        TOE

6                 9          a
12               0          c
10               0          d
4                 9          a

 

is actual, literal values in my db.  TOE is an encrypted id that is about 80 characters long.

I want to sum db column TICS where column TACS equals 0 (12 + 10)

I want to sum unique values of TACS / or group by TOE (9)

I want to add the 2 numbers together. (equals 31)

Because I am a beginner, I do not know the best approach.  If anyone can help (and play nice) I would be grateful.

Edited by mrscoop
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.