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

发布于 2024-12-11 13:45:55 字数 2559 浏览 0 评论 0原文

我有两个表 -

INCOME

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

Expense

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

现在我想要这些结果

Result

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

ma​​in_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

I have two tables -

INCOME

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

Expense

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

Now I want these results

Result

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

Remember: All this work I have to done in SQL

I have done the total income, total expense but what is the better way for difference?

----- edited......

I have tried this

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";

edited second time

Okey, please understand the original logic. Table expense have lot of records relation with project and table income also have the same now i want result of total income per project, total expense per project and difference per project

write your answer Using these fields

INCOME

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

Expense

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

main_head or project

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

Note in_source = mh_id

At this time i am using the following queries

for total income per project

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

total expense per project

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

SOLVED BY ME....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝天 2024-12-18 13:45:55

这个 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

This SQL should work for you:

I am assuming there is a 1 to 1 relationship between the income and expense table. Otherwise you might want to use some left joins.

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

Updated Answer that reflects the 0 - many incomes and 0 - many expenses possible:

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
娇俏 2024-12-18 13:45:55
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

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

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

EDIT: we have all been wrong. Because the joins without subqueries will join all matches, it gives us duplicate results. Here is the correct answer and the right results, otherwise you are going to be getting double values:

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

I kept up my original answer so that they can be compared. The second query is correct.

秋心╮凉 2024-12-18 13:45:55

您的尝试和几个答案面临的问题是您正在将数据集连接在一起。

例如,项目 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

The problem faced by your attempt and several answers is that you are joining sets of data together.

Project 2, for example, has 3 income records and 2 expense records. You don't actually want to join these together, you'll get 6 records as a result...

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

As you can see, this will cause serious duplication.

There are a few standard approaches. Such as Correlated Sub-Queries or Inline Views.

Correlated SubQuery...

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

Inline Views

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

EDIT

A correction to Hamidam's attempt at using 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
野生奥特曼 2024-12-18 13:45:55
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文