从借方和贷方表计算账户余额

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从借方和贷方表计算账户余额相关的知识,希望对你有一定的参考价值。

我有一个像这样的帐户表

+-----------+--------------+
|     fld_id|         name |
+-----------+--------------+
|         1 |         Bank1|
|         2 |         Bank2|
|         4 |         Bank3|
+-----------+--------------+

收入收入表就像这样

+-----------+--------------+---------------+-------------+
| fld_id    | fld_type     | fld_account id| fld_amount  |
+-----------+--------------+---------------+-------------+
|         1 |         Salry|            1  |     400     |
|         2 |         Rent |            2  |     500     |
|         4 |       Others |            1  |    1000     |
+-----------+--------------+---------------+-------------+

付款表就像这样

+-----------+--------------+---------------+-------------+
| fld_id    | fld_type     | fld_account id| fld_amount  |
+-----------+--------------+---------------+-------------+
|         1 |         Food |            2  |     200     |
|         2 |    Entertain |            2  |     300     |
|         4 |    Transport |            1  |     400     |
+-----------+--------------+---------------+-------------+

我希望账户的最终余额表与收入,费用和余额之和如此表 -

+-----------+--------------+---------------+-------------+
| account   | Income       | Expence       | Balance     |
+-----------+--------------+---------------+-------------+
|     Bank1 |         1400 |          400  |    1000     |
|     Bank2 |          500 |          500  |       0     |
|     Bank3 |            0 |            0  |       0     |
+-----------+--------------+---------------+-------------+

到目前为止,我写这个查询并获得收入和支出,但没有找到任何计算余额的方法,我的查询和结果是--query

SELECT fld_account as account, Income, Expense
from tbl_accounts
LEFT JOIN (SELECT fld_account_id, SUM(fld_amount) as Income FROM tbl_revenue tr GROUP BY tr.fld_account_id) tc on fld_id=tc.fld_account_id
left JOIN (SELECT fld_account_id, SUM(fld_amount) as Expense FROM tbl_payment tp GROUP BY tp.fld_account_id) td on fld_id=td.fld_account_id

结果如下 -

+-----------+--------------+---------------+
| account   | Income       |   Expense     | 
+-----------+--------------+---------------+
|     Bank1 |         1400 |         400   |
|     Bank2 |          500 |         500   |
|     Bank3 |         Null |         Null  |
+-----------+--------------+---------------+

如何计算余额表格付款和收入表格并将其加入我的帐户表格?非常感谢任何帮助。

答案

只需使用coalesce()

SELECT fld_account as account, COALESCE(Income, 0) as Income,
       COALESCE(Expense, 0) as Expense,
       ( COALESCE(Income, 0) - COALESCE(Expense, 0) ) as balance
FROM tbl_accounts LEFT JOIN
     (SELECT fld_account_id, SUM(fld_amount) as Income
      FROM tbl_revenue tr
      GROUP BY tr.fld_account_id
     ) tc 
     ON fld_id = tc.fld_account_id LEFT JOIN
     (SELECT fld_account_id, SUM(fld_amount) as Expense
      FROM tbl_payment tp
      GROUP BY tp.fld_account_id
     ) td 
     ON fld_id = td.fld_account_id;

COALESCE()是ANSI标准函数,它返回第一个非NULL参数。

以上是关于从借方和贷方表计算账户余额的主要内容,如果未能解决你的问题,请参考以下文章

如何动态从两个 SQL 表借方和贷方中获取余额

如何计算总账中的贷方、借方和余额?

如何计算贷方和借方的余额?

sap中借方为啥用s

收藏SAP记账码

设置和登记现金银行存款日记账怎么操作