来自两个单独表的SQL结果(差异)

时间:2011-10-25 15:29:17

标签: mysql sql subquery sum

我有两张桌子 -

所得

Id - ProjectId - inAmount 
1  - 2              200   
2  - 2              100
3  - 1              100
4  - 2              100
5  - 1              200

费用

Id - ProjectId - exAmount
1  - 2           50        
2  - 1           100
3  - 2           120
4  - 1           70

现在我想要这些结果

结果

ProjectId - Total Income - Total Expense - Difference 
1           result         result          result
2           result         result          result

请记住:我必须在SQL中完成所有这些工作

我已经完成了总收入,总费用但差异的更好方法是什么?

-----编辑......

我试过这个

SELECT sum(ex_amount) as expense, 
       sum(in_amount) as income,  
       sum(in_amount) - sum(ex_amount) as Difference,  
       project_name
FROM expense, 
     project, 
     income
WHERE expense.projectId = Project.id 
  AND income.ProejctId = Project.id
group by mh_name";

第二次编辑

哦,请理解原来的逻辑。表费用与项目有很多记录关系,表收入也有相同现在我想要每个项目的总收入,每个项目的总费用和每个项目的差异的结果

使用这些字段撰写答案

所得

in_id  - in_source -  in_amount 
    1  - 2              200   
    2  - 2              100
    3  - 1              100
    4  - 2              100
    5  - 1              200

费用

ex_id - mh_id -     ex_amount
    1  - 2           50        
    2  - 1           100
    3  - 2           120
    4  - 1           70

main_head 或项目

mh_id -  mh_name
    1  -  abc
    2  -  ase
    3  -  czz
    4  -  xys

注意in_source = mh_id

此时我正在使用以下查询

每个项目的总收入

SELECT sum(in_amount) as amount, mh_name FROM income, main_head WHERE income.in_source = main_head.mh_id group by mh_name Order By amount desc

每个项目的总费用

SELECT sum(ex_amount) as amount, mh_name FROM expense, main_head WHERE expense.mh_id = main_head.mh_id group by mh_name Order By amount desc

由我解决....

SELECT
mh_name,
 income - expense AS difference 
FROM 
(SELECT sum(in_amount) AS income, in_source FROM income GROPU BY in_source) AS t1, 
(SELECT sum(ex_amount) AS expense, mh_id FROM expense GROUP BY mh_id) AS t2, 
(SELECT * FROM main_head) AS t3 
WHERE 
t1.in_source = t2.mh_id 
AND
t1.in_source=t3.mh_id

4 个答案:

答案 0 :(得分:2)

select 
    i.ProjectId, 
    sum(i.inamount) as TotalIncome, 
    sum(e.examount) as TotalExpense, 
    sum(i.inamount) - sum(e.examount) as Difference
from income i
inner join expense e
on i.projectid = e.projectid
group by i.projectid

编辑:我们都错了。因为没有子查询的连接将连接所有匹配,它会给我们重复的结果。这是正确的答案和正确的结果,否则你将获得双重值:

select 
    i.projectid,
    i.inamount as Income,
    e.examount as Expense,
    i.inamount - e.examount as [Difference]
from
(
    select projectid, SUM(inamount) as inamount
    from income
    group by projectid
) as i
full outer join
(
    select projectid, SUM(examount) as examount
    from expense
    group by projectid
) as e
on i.projectid = e.projectid

我保留了原来的答案,以便对它们进行比较。第二个查询是正确的。

答案 1 :(得分:2)

此SQL应该适合您:

我假设收入和支出表之间存在1比1的关系。否则你可能想要使用一些左连接。

SELECT i.ProjectId, 
       sum(inAmount) AS "Total Income",
       sum(exAmount) AS "Total Expense",
       (sum(inAmount) - sum(exAmount)) AS "Difference"
FROM   Income i
JOIN   Expense e
ON     i.project_id = e.project_id
GROUP BY i.ProjectId

更新的答案反映了0 - 许多收入和0 - 可能的许多费用:

SELECT i.ProjectId, 
       i.income AS "Total Income",
       e.expense AS "Total Expense",
       (i.income - e.expense) AS "Difference"
FROM   (SELECT ProjectId, sum(inAmount) AS income FROM Income GROUP BY ProjectId) i
FULL OUTER JOIN   (SELECT ProjectId, sum(exAmount) AS expense FROM Income GROUP BY ProjectId) e
ON     i.project_id = e.project_id

答案 2 :(得分:1)

您的尝试和几个答案所面临的问题是您正在加入数据。

例如,项目2有3个收入记录和2个费用记录。你实际上并不想将它们加在一起,结果你会得到6条记录......

Id - ProjectId - inAmount        Id - ProjectId - exAmount
1  - 2              200           1   2           50
2  - 2              100           1   2           50
4  - 2              100           1   2           50
1  - 2              200           3   2           120
2  - 2              100           3   2           120
4  - 2              100           3   2           120

如您所见,这将导致严重的重复。

有一些标准方法。例如相关子查询或内联视图。


相关子查询......

SELECT
  *,
  (SELECT SUM(inAmount) FROM income  WHERE projectId = project.Id) AS income,
  (SELECT SUM(exAmount) FROM expense WHERE projectId = project.Id) AS expense
FROM
  project


内联视图

SELECT
  project.Id,
  income.inAmount,
  expense.exAmount
FROM
  project
LEFT JOIN
  (SELECT projectID, SUM(inAmount) AS inAmount FROM income  GROUP BY projectID) AS income
    ON income.projectID = project.ID
LEFT JOIN
  (SELECT projectID, SUM(exAmount) AS exAmount FROM expense GROUP BY projectID) AS expense
    ON expense.projectID = project.ID

修改

修正Hamidam试图使用UNION ......

SELECT
  projectID,
  SUM(inAmount),
  SUM(exAmount)
FROM
(
  SELECT projectID,      inAmount, 0 AS exAmount FROM income  GROUP BY projectID
  UNION ALL
  SELECT projectID, 0 as inAmount,      exAmount FROM expense GROUP BY projectID
)
  AS data
GROUP BY
  projectID

答案 3 :(得分:0)

SELECT ge.id AS ProjectID 
     , TotalIncome
     , TotalExpense
     , TotalIncome - TotalExpense AS Difference
FROM 
    ( SELECT p.id
           , COALESCE(SUM(i.in_amount), 0) AS TotalIncome
      FROM project p
        LEFT JOIN income i
          ON i.projectid = p.id
      GROUP BY p.id
    ) ge
  JOIN
    ( SELECT p.id
           , COALESCE(SUM(e.ex_amount), 0) AS TotalExpense
      FROM project p
        LEFT JOIN expense e
          ON e.projectid = p.id
      GROUP BY p.id
    ) gi
    ON ge.id = gi.id