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

Posted

技术标签:

【中文标题】如何计算总账中的贷方、借方和余额?【英文标题】:How Can Calculate Credits,Debits & Balance in the general ledger? 【发布时间】:2016-11-29 06:42:09 【问题描述】:

余额未显示正确值

MySQL 数据库表:

CREATE TABLE `transactions` (
  `trx_id` int(11) NOT NULL AUTO_INCREMENT,
  `trx_type` enum('debit','credit') DEFAULT NULL,
  `trx_amount` float DEFAULT NULL,
  `trx_description` mediumtext NOT NULL,
  `trx_date` date NOT NULL,
  `purpose_id` int(11) NOT NULL,
  `staff_id` int(11) DEFAULT NULL,
  `admin_id` int(11) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`trx_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

/*表transactions的数据*/

insert  into `transactions`(`trx_id`,`trx_type`,`trx_amount`,`trx_description`,`trx_date`,`purpose_id`,`staff_id`,`admin_id`,`created_at`,`updated_at`)
 values (1,'credit',100,'annual tour','2016-01-01',2,7,2,'2016-11-24 10:28:35','2016-11-29 12:21:08'),
 (2,'debit',200,'Product Sale','2016-11-19',6,5,2,'2016-11-24 10:33:02','2016-11-29 12:21:12'),
 (3,'debit',250,'Product Sale','2016-11-19',6,4,2,'2016-11-24 10:33:11','2016-11-29 12:21:43'),
 (4,'credit',300,'Product Sale','2015-01-27',6,4,2,'2016-11-24 10:33:14','2016-11-29 12:21:53'),
 (5,'credit',450,'Product Sale','2016-01-29',5,2,2,'2016-11-24 10:33:17','2016-11-29 12:21:58'),
 (6,'debit',210,'Product Sale','2016-11-19',6,4,2,'2016-11-24 10:33:20','2016-11-29 12:22:17'),
 (7,'credit',350,'Internal','2016-11-30',14,3,1,'2016-11-24 13:04:04','2016-11-29 12:22:28');

MySQL 查询:

 SELECT trx_id,staff_id
     , SUM(COALESCE(CASE WHEN trx_type = 'debit' THEN trx_amount END,0)) total_debits
     , SUM(COALESCE(CASE WHEN trx_type = 'credit' THEN trx_amount END,0)) total_credits

     , SUM(COALESCE(CASE WHEN trx_type = 'debit' THEN trx_amount END,0)) 
     - SUM(COALESCE(CASE WHEN trx_type = 'credit' THEN trx_amount END,0)) balance 
  FROM erp_transactions  GROUP BY staff_id  HAVING balance <> 0 ORDER BY trx_id;

输出结果:

【问题讨论】:

出了什么问题,你想要什么? 你能解释一下你到底想做什么吗? 您的意思是无效的借方和贷方值在逻辑上显示为前两行不应为负数? @hunter balance 2nd row will be +100 余额是否为流动余额? 【参考方案1】:
 select s.*,
        s.debit - s.credit as Balance,
        @RunningBalance:= @RunningBalance + s.debit - s.credit RunningBalance
from 
(
select min(trx_id) trx_id,t.staff_id,
    sum(case when trx_type = 'debit' then trx_amount else 0 end) as Debit,
    sum(case when trx_type = 'credit' then trx_amount else 0 end) as Credit
from  trans t
group by staff_id 
order by trx_id
) s,
(Select @RunningBalance:=0) rb
order by s.trx_id

结果

    +--------+----------+-------+--------+---------+----------------+
| trx_id | staff_id | Debit | Credit | Balance | RunningBalance |
+--------+----------+-------+--------+---------+----------------+
|      1 |        7 |     0 |    100 |    -100 |           -100 |
|      2 |        5 |   200 |      0 |     200 |            100 |
|      3 |        4 |   460 |    300 |     160 |            260 |
|      5 |        2 |     0 |    450 |    -450 |           -190 |
|      7 |        3 |     0 |    350 |    -350 |           -540 |
+--------+----------+-------+--------+---------+----------------+

【讨论】:

是的,我现在得到的确切值

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

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

使用Mysql从不同名称的不同表中选择值作为贷方和借方并计算余额

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

sap中借方为啥用s

多个 has_one 和 has_many 关系

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