为什么使用SUM()得到不正确的结果

时间:2014-12-16 03:01:02

标签: mysql sql

我一直在尝试编写一个总结列的SQL查询。这是查询:

SELECT `p`.`project_id`          AS `project_id`, 
       Sum(`r`.`expected_cost`)  AS `total_expected_cost`, 
       Sum(`r`.`expected_delay`) AS `total_expected_delay`, 
       Sum(`resp`.`cost`)        AS `project_total_mitigation_cost` 
FROM   ( ( ( `risks` `r` 
             JOIN `tasks` `t` 
               ON (( `t`.`task_id` = `r`.`task_id` )) ) 
           JOIN `projects` `p` 
             ON (( `t`.`project_id` = `p`.`project_id` )) ) 
         LEFT JOIN `responses` `resp` 
                ON (( `resp`.`risk_id` = `r`.`risk_id` )) ) 
GROUP  BY `p`.`project_id` 

列的求和结果太高了。 expected-cost, expected_delay, cost不能是NULL

示例数据:

**************************
        projects
**************************
project_id    project_name
    1             alpha
    2             beta
    3             gamma
    4             delta

*******************************************************
                    tasks
*******************************************************
    task_id    project_id     name        
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
     5              1         mobile        
     45             1         keyboard           
     9              3         bottle          
     20             4         card           

*********************************************************
                        risks
*********************************************************
    risk_id    task_id     expected_cost    expected_delay    
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     1           5            1400.00        5.0
     2           45           300.00         4.0  
     3           9            10.00          10.0
     4           20           0.00           100.0

*******************************************************
                        responses
*******************************************************
    risk_id    response_id     cost        
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
     1         7              1500.00        
     2         9              900.00           
     3         13             80.00          
     4         2              1.00           

*********************************************************************************************
                                 desired result of query
*********************************************************************************************
  project_id    total_expected_cost     total_expected_delay   project_total_mitigation_costs        
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     1              1700.00                        9.0                     2400.00
     2              0.00                           0.0                     0.00
     3              10.00                          10.0                    80.00
     4              0.00                           100.0                   1.00

关于样本数据的一点解释:

项目的任务和任务都有风险,风险也有回应。风险有预期成本和预期延迟。回应有成本。

我不知道我哪里出错了。非常感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

要调试此操作,请将select子句更改为count(*)

  • 然后在第一张表上运行计数
  • 一次添加一个连接
  • 如果计数增加超过预期,您将知道哪个JOIN导致 问题。

一旦解决了连接并且count(*)正确,请将常规select子句放回

答案 1 :(得分:0)

我不确定是什么导致您的金额返回更高的数字,但您的from子句中的括号可能会被取消。删除它们是最好的,因为它们不是必需的。

此外,您应该从projects表格中进行选择 左边连接其余的表,因为你想要每个项目一行。

select p.project_id,
    sum(expected_cost) total_expected_cost,
    sum(expected_delay) total_expected_delay,
    sum(cost) project_total_mitigation_cost
from projects p
left join tasks t on t.project_id = p.project_id
left join responses r on r.project_id = p.project_id
group by p.project_id