?
Solved

mysql sum retrieving expected results+total results

Posted on 2009-06-30
11
Medium Priority
?
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
11 Comments
 
LVL 32

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 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 24760427
Which table are the ability_* columns in?
0
 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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