简体   繁体   English

查询父表时如何获取子表列的SUM?

[英]How to get SUM of a column of a child table when query parent table?

I have two tables我有两张桌子

Account Table (account)账户表(账户)

id      name
1       Account 1
2       Account 2
3       Account 3
4       Account 2

Transaction Table (transaction)交易表(交易)

id      account_id     type      amount     datetime
1       1              credit    500        2020-04-01 06:00:00
2       1              credit    300        2020-04-01 06:00:00
3       2              credit    100        2020-04-01 06:00:00
4       2              debit     50         2020-04-01 06:00:00
5       1              debit     600        2020-04-01 06:00:00
6       3              credit    1000       2020-04-01 06:00:00
7       1              credit    100        

My target is to get account id, name, balance in one query.我的目标是在一个查询中获取帐户id, name, balance The balance will be calculated from the transaction table as SUM of Credit Amount - SUM of Debit Amount for a given account.余额将从交易表中计算SUM of Credit Amount - SUM of Debit Amount总和SUM of Credit Amount - SUM of Debit Amount给定帐户SUM of Credit Amount - SUM of Debit Amount总和。

Target Output目标输出

   id      name          balance
   1       Account 1     300
   2       Account 2     50
   3       Account 3     1000
   4       Account 4     0

Possible Query可能的查询

SELECT id, name, ((SELECT SUM(amount) FROM transaction WHERE type = 'credit' AND account_id = {ACCOUNT_ID} ) - (SELECT SUM(amount) FROM transaction WHERE type = 'debit' AND account_id = {ACCOUNT_ID} )) as balance

Is it possible to do this in one query and If yes How to do it.是否可以在一个查询中执行此操作,如果是,如何执行此操作。

You could do the aggregation in a derived table to avoid the issues of having to group by a lot of fields in the top level.您可以在派生表中进行聚合,以避免必须按顶级中的许多字段进行分组的问题。 For example:例如:

SELECT a.id, a.name, COALESCE(b.balance, 0) AS balance
FROM account a
LEFT JOIN (
  SELECT account_id,
         SUM(CASE WHEN type='credit' THEN amount ELSE 0 END) -
         SUM(CASE WHEN type='debit' THEN amount ELSE 0 END) AS balance
  FROM transaction
  GROUP BY account_id
) b ON b.account_id = a.id

Output:输出:

id  name        balance
1   Account 1   300
2   Account 2   50
3   Account 3   1000
4   Account 2   0

Demo on SQLFiddle SQLFiddle 上的演示

You need a left join of account to transaction so you can group by each account and conditionally sum the amount depending on the type :您需要将account left jointransaction以便您可以按每个帐户分组并根据type有条件地总结amount

select 
  a.id, a.name,
  sum(case when type = 'credit' then 1 else -1 end * coalesce(t.amount, 0)) balance
from account a left join transaction t
on t.account_id = a.id
group by a.id, a.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对按父表中的列分组的子表中的行进行求和 - How to sum rows from a child table grouped by a column in parent table 当第二个子表有记录时,如何通过父表字段对子表的字段求和? - How to sum a field of a child table by a parent table field when a second child table has records? 按子表中列的 SUM 对父表中的行进行排序 - Ordering rows in a parent table by SUM of column in a child table 如何从另一个表中获取子列的总和 - how to get the sum of a child column from another table Codeigniter查询:当我使用sum Select函数从另一个表中获取数据时,如何从表中的列中获取所有记录 - codeigniter query: how to take all the record from a column in a table when i use sum Select function to get data from another table 如何从父表获取ID,从子表获取JOIN以运行查询。 - How do i get the ID from parent table and JOIN from child table to run the query. MySQL如何使子表的列指向父表? - MySQL how to make a column of child table point to the parent table? 如何在sql查询中将子表声明为父表? - How to declare a child table as parent table in a sql query? 获取数据单表形式的父子查询pos_parent_id表作为子ID - get data single table form parent child query pos_parent_id table as child id 限制父表而不是子表的MySQL查询 - MySQL query with limit on parent table not on child table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM