dongyuchen9276 2016-11-29 06:42
浏览 24
已采纳

如何计算总帐中的贷记,借方和余额?

Balance Not showing proper value

MySQL Database Table:

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;

/*Data for the table 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 Query:

 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;

OutPut Result:

enter image description here

  • 写回答

1条回答 默认 最新

  • dongmu6225 2016-11-29 09:43
    关注
     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
    

    Result

        +--------+----------+-------+--------+---------+----------------+
    | 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 |
    +--------+----------+-------+--------+---------+----------------+
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据