Solved

mysql sum retrieving expected results+total results

Posted on 2009-06-30
11
308 Views
Last Modified: 2012-05-07
Ok this may require too much detail to explain but perhaps someone knows the general reason this happens..
I am returning sums which basically is the number of records that have fields set to 1.  There are 5 fields that may be zero or 1 in a file which is referenced by another file with a lot of records.
The error is each of the 5 results returns not the sum expected but the sum expected+the total #of records that has any field set.
That is if there were 20 total product_ids that met the select requirements and of these 20 the # that had each of the 5 abilities was (13,20,4,1,19) what actually is returned is (33,40,24,1,39)
this is easy enough to fix because I know the total number of results before hand so I can just subtract it from the returned results, but Id rather have the mysql correct.

Perhaps someone knows generally why this happens.
Anyway the code is

select sum(ability_1) as level_1, sum(ability_2) as level_2, sum(ability_3) as level_3, sum(ability_4) as level_4, sum(ability_5) as level_5 from products p left join manufacturers m using(manufacturers_id) left join products_description pd on p.products_id=pd.products_id left join model_information_table mit on mit.model_number=p.products_model where p.products_status = '1' and p.products_quantity>0 and (pd.products_name like '%lightning%' or p.products_model = 'lightning' or p.products_id = 'lightning' or m.manufacturers_name like '%lightning%')
0
Comment
Question by:levelninesports
  • 5
  • 2
11 Comments
 
LVL 31

Expert Comment

by:awking00
ID: 24757182
Can you post the relevant table structures with some sample data?
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 24758632
You've probably got a relationship between 2 tables that needs joining with more than 1 column. The way to pick this up is to temporarily remove the grouping from the query. You haven't specified included the "group by" clause at the end of your query, but MySQL knows to do it anyway when you are selecting sum()'s.

Can you run this, and post the output?


select ability_1,

       ability_2, 

       ability_3, 

       ability_4, 

       ability_5 

  from products p left join manufacturers m using(manufacturers_id) 

                  left join products_description pd on p.products_id=pd.products_id 

                  left join model_information_table mit on mit.model_number=p.products_model 

  where p.products_status = '1' 

    and p.products_quantity>0 

    and (pd.products_name like '%lightning%' 

         or p.products_model = 'lightning' 

         or p.products_id = 'lightning' 

         or m.manufacturers_name like '%lightning%')

Open in new window

0
 

Author Comment

by:levelninesports
ID: 24760397
Terry,
The result is an array in which many items are duplicated which was causing the summation error.. I cannot figure out what code is missing but adding dinstinct(p.products_id) to the select clause returns a valid array.  THis is ok but then I have to run a summation php loop
            while ($refine_result=tep_db_fetch_array($result)){
                  echo $refine_result['products_id'].'<br/>';
                  $sum_array[1] += $refine_result['ability_1'];
                  $sum_array[2] += $refine_result['ability_2'];                  
                  $sum_array[3] += $refine_result['ability_3'];                  
                  $sum_array[4] += $refine_result['ability_4'];                  
                  $sum_array[5] += $refine_result['ability_5'];
            }      
which isnt as slow as running multiple sqls, but still seems avoidable.. here is a gist of what is going on
there may be muliple p.products_id that have the same p.products_model.. these will all be returned in the select and were part of the original summation (which is correct) however is three products_id results were returned for the same value p.products_model then each of these would actually be returned 3 times in the select clause

I understand the problem exactly (thanks for leading me there) and have a working solution (your code with the added part to select above) but still thing there must be a simple clarification so that the mysql query can returned the sum result as I was originally intending
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24760427
Which table are the ability_* columns in?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:levelninesports
ID: 24760544
model_information_table
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24760738
Then perhaps this?
select sum(ability_1) as level_1,

       sum(ability_2) as level_2,

       sum(ability_3) as level_3,

       sum(ability_4) as level_4,

       sum(ability_5) as level_5

  from model_information_table mit 

  where exists (select p.* 

                  from products p left join manufacturers m using(manufacturers_id) 

                                  left join products_description pd on p.products_id=pd.products_id 

                  where p.products_model = mit.model_number

                    and p.products_status = '1' 

                    and p.products_quantity>0 

                    and (pd.products_name like '%lightning%' 

                         or p.products_model = 'lightning' 

                         or p.products_id = 'lightning' 

                         or m.manufacturers_name like '%lightning%')

               )

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24930718
I helped the asker to understand the issue and get it resolved:
"I understand the problem exactly (thanks for leading me there) and have a working solution (your code with the added part to select above)"
as well as providing an additional solution when they provided further clarification for the problem.

It took substantial effort to go through that process, and I was successful. I deserve the points. Thanks.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24930726
I helped the asker to understand the issue and get it resolved:
"I understand the problem exactly (thanks for leading me there) and have a working solution (your code with the added part to select above)"
as well as providing an additional solution when they provided further clarification for the problem.

It took substantial effort to go through that process, and I was successful. I deserve the points. Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now