Solved

mysql sum retrieving expected results+total results

Posted on 2009-06-30
11
319 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 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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