[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

SQL SUM and Join

Asked by flow79 in PHP Scripting Language

Tags: sql, sum, join

Hello all!

I am working to create a small report from our data to determine the profit per job.  In doing so, I have two tables customer_costs and actual_costs.  The profit can be found by adding the total_price fields per job_number in each table, and then subtracting these sums.  You will see the SQL syntax below.

My issue is this: Whenever I run this query, the answer is double the actual number desired.  I will display an example of each table to illustrate the correct numbers below, and then show you what is actually output by the query.

table: customer_costs
------------------------------------------------------------------
ID  |   job_number  |     total_price
------------------------------------------------------------------
1   |         46           |       135.00
------------------------------------------------------------------
2   |         46           |       247.50
------------------------------------------------------------------

table: actual_costs
------------------------------------------------------------------
ID  |   job_number  |     total_price
------------------------------------------------------------------
1   |         46           |       101.25
------------------------------------------------------------------
2   |         46           |       180.00
------------------------------------------------------------------

As you can see the total customer_cost for job_number 46 is: 382.50.  And the total actual_cost is: 281.25

Thus the profit should be: 382.50 - 281.25 = 101.25

The SQL statement below gives me: 202.50 (double the real amount).  Anyone know why the sum is doubled?  is it due to how many tables are in the join?  If so, how do I work around this (other than simply sub-querying each)?

SELECT SUM(cc.total_price - ac.total_price) FROM customer_costs cc, actual_costs ac WHERE cc.job_number = ac.job_number AND cc.job_number = '46'

Thanks!
[+][-]04/20/07 07:34 AM, ID: 18946699Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/20/07 07:37 AM, ID: 18946724Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: PHP Scripting Language
Tags: sql, sum, join
Sign Up Now!
Solution Provided By: DanielWilson
Participating Experts: 3
Solution Grade: A
 
[+][-]04/20/07 07:39 AM, ID: 18946746Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/20/07 08:37 AM, ID: 18947247Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/20/07 09:04 AM, ID: 18947442Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81