# Problem using SUM() with LEFT JOIN

2 replies to this topic

### #1 gunslinger008

gunslinger008
• New Members
• Newbie
• 2 posts

Posted 30 October 2003 - 02:38 AM

I\'m using MySQL v4.0.13, and I have the following tables: PROJECTS, BUDGETS, EXTRA_COSTS. Each project can have multiple (or zero) budgets and multiple (or zero) extra costs. I\'m trying to get the budget and extra cost totals for a particular project using the following query:

`SELECT SUM(budget_tot) AS budget_total, SUM(extrac_tot) AS extracost_totalFROM PROJECTSLEFT JOIN  BUDGETS      ON budget_proj_id = proj_idLEFT JOIN  EXTRA_COSTS  ON extrac_proj_id = proj_idWHERE proj_id = 143`

The problem is when a project has a more than one budget or more than one extra cost each amount is multiplied by the number of rows in the other table. For example, if a project has five budgets and two extra costs, each budget amount would be included twice and each extra cost amount would be included five times.

I realize what\'s happening here, but it\'s not what I expected (and obviously not what I\'d like to happen). I have worked out the following trick to get the right numbers, but it\'s a bit of a hack:

`SELECT ( SUM(budget_tot) / IF(COUNT(DISTINCT extrac_id)=0, 1, COUNT(DISTINCT extrac_id)) ) AS budget_total,       ( SUM(extrac_tot) / IF(COUNT(DISTINCT budget_id)=0, 1, COUNT(DISTINCT budget_id)) ) AS extracost_totalFROM PROJECTSLEFT JOIN  BUDGETS      ON budget_proj_id = proj_idLEFT JOIN  EXTRA_COSTS  ON extrac_proj_id = proj_idWHERE proj_id = 143`

Does anyone know of a better way to do this without resorting to separate queries? It would be nice to have something like SUM(DISTINCT ...). Does anyone know if the LEFT JOIN behavior has changed in MySQL v4.1?

Thanks,
Sean

### #2 Alien

Alien
• Members
• Newbie
• 9 posts

Posted 03 November 2003 - 08:01 AM

you can try this

SELECT SUM(budget_tot) AS budget_total, SUM(extrac_tot) AS extracost_total
FROM PROJECT, SBUDGETS, EXTRA_COSTS
WHERE extrac_proj_id = proj_id AND
budget_proj_id = proj_id AND
proj_id = 143

### #3 gunslinger008

gunslinger008
• New Members
• Newbie
• 2 posts

Posted 03 November 2003 - 09:08 AM

you can try this

SELECT SUM(budget_tot) AS budget_total, SUM(extrac_tot) AS extracost_total
FROM PROJECT, SBUDGETS, EXTRA_COSTS
WHERE extrac_proj_id = proj_id AND
budget_proj_id = proj_id AND
proj_id = 143

Thanks for the suggestion, but unfortunately it doesn\'t work. :/ When a project has more than one budget or extra cost the same thing happens as I described. And, without the LEFT JOINs, if a project has zero budgets or extra costs then both sums become null.

I\'ve decided that what\'s needed is formal subqueries, so I\'ll just have go with separate queries until MySQL v4.1 is ready for production use. We migrated from Oracle a while ago, and while I can\'t test it, I believe Oracle does (left) outer joins in a subquery-like fashion.

Cheers,
Sean

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users