我有以下表格和关系
这里是样本数据-
我尝试了以下查询-
SELECT project.*, SUM(schedule.amount) as sch_total, SUM(expense.amount) as expense_total, SUM(bill.amount) as bill_total from project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN expense on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id
这是查询结果-
但是“ bill_total”应该是200,而不是400。我在查询中错过了什么?
您的费用表具有相同ID的多行。该表必须首先聚合,然后在连接中使用-