来自两个单独表的 SQL 结果(差异)
我有两个表 -
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
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个 SQL 应该适合你:
我假设收入和支出表之间存在 1 对 1 的关系。否则你可能想使用一些左连接。
更新的答案反映了 0 - 许多收入和 0 - 许多可能的支出:
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.
Updated Answer that reflects the 0 - many incomes and 0 - many expenses possible:
编辑:我们都错了。因为不带子查询的连接将连接所有匹配项,所以它会给我们重复的结果。这是正确的答案和正确的结果,否则您将得到双重值:
我保留了原来的答案,以便可以比较它们。第二个查询是正确的。
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:
I kept up my original answer so that they can be compared. The second query is correct.
您的尝试和几个答案面临的问题是您正在将数据集连接在一起。
例如,项目 2 有 3 条收入记录和 2 条支出记录。您实际上并不想将它们连接在一起,结果您将得到 6 条记录...
如您所见,这将导致严重的重复。
有一些标准方法。例如相关子查询或内联视图。
相关子查询...
内联视图
编辑
对 Hamidam 尝试使用 UNION 的更正...
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...
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...
Inline Views
EDIT
A correction to Hamidam's attempt at using UNION...