Solved

mysql sum retrieving expected results+total results

Posted on 2009-06-30
11
320 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
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. . .
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

687 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