MySQL Open Balance信用借方余额

时间:2015-03-03 09:24:00

标签: mysql

我有一张名为" Stock"如下图所示。

+-----------+--------------+---------------+---------+
| client_id |       date   |       credit  |    debit|
+-----------+--------------+---------------+---------+
|         1 |   01-01-2015 |            50 |       0 |
|         2 |   01-01-2015 |           250 |       0 |
|         2 |   01-01-2015 |           500 |       0 |
|         2 |   02-01-2015 |             0 |     500 |
|         1 |   02-01-2015 |             0 |      40 |
|         1 |   02-01-2015 |             0 |      80 |
|         3 |   05-01-2015 |          3000 |       0 |
|         2 |   06-01-2015 |             0 |     350 |
|         4 |   06-01-2015 |             0 |    1000 |
|         4 |   06-01-2015 |             0 |    2000 |
|         4 |   07-01-2015 |           500 |       0 |
|         5 |   07-01-2015 |           500 |       0 |
|         5 |   08-01-2015 |           500 |       0 |
|         1 |   09-01-2015 |             0 |     100 |
+-----------+--------------+---------------+---------+

我期待的结果是:

+---------+-----------+-------------+--------+---------+----------+
|client_id|  date     |Open_Balance | credit |  debit  | balance  |
+---------+-----------+-------------+--------+---------+----------+
|       1 |01-01-2015 |           0 |     50 |       0 |       50 |
|       1 |02-01-2015 |          50 |      0 |      40 |       10 |
|       1 |02-01-2015 |          10 |      0 |      80 |      -70 |
|       1 |09-01-2015 |         -70 |      0 |     100 |     -170 |
|       2 |01-01-2015 |           0 |    250 |       0 |      250 |
|       2 |01-01-2015 |         250 |    500 |       0 |      750 |
|       2 |02-01-2015 |         750 |      0 |     500 |      250 |
|       2 |06-01-2015 |         250 |      0 |     350 |     -100 |  
|       3 |05-01-2015 |           0 |   3000 |       0 |     3000 |
|       4 |06-01-2015 |           0 |      0 |    1000 |    -1000 |
|       4 |06-01-2015 |       -1000 |      0 |    2000 |    -3000 |
|       4 |07-01-2015 |       -3000 |    500 |       0 |    -2500 |
|       5 |07-01-2015 |           0 |    500 |       0 |      500 |
|       5 |08-01-2015 |         500 |    500 |       0 |     1000 |
+---------+-----------+-------------+--------+---------+---- -----+

我需要余额和'未结余额'由client_id和日期顺序计算,如上所示。请帮忙。

2 个答案:

答案 0 :(得分:2)

首先为开放平衡和平衡设置两个变量;

mysql> set @balance = 0;

mysql> set @openBalance = 0;

然后设置id变量

mysql> set @id := (select client_id from Stock order by client_id asc limit 1);

现在运行此查询

select client_id,date,IF(client_id=@id,@balance:=@balance,@balance:=0),
@openBalance:=@balance as OpenBalance,credit,debit,@balance:=(credit+@openBalance)-debit as 
bal,@id:=client_id from Stock order by client_id,date;

是的,因为某些数据具有相同的日期和ID,查询可能会以不同的方式工作,因此请在您的表定义中进行一些更改,而不是将日期设置为日期时间,然后进行相应的排序。

答案 1 :(得分:2)

这是你怎么做的..

select 
s.client_id,
s.date,
s.op_balance as Open_Balance,
s.credit,
s.debit,
s.balance
from
(
  select 
  t.client_id,
  t.date,
  t.credit,
  t.debit,
  @tot_credit := if(@prev_client = t.client_id, @tot_credit + t.credit,t.credit) as tot_cred,
  @tot_debit := if(@prev_client = t.client_id,@tot_debit + t.debit,t.debit) as tot_deb,
  @cur_bal := if(@prev_client = t.client_id, @tot_credit - @tot_debit,t.credit-t.debit) as balance,
  (@cur_bal + t.debit) - t.credit as op_balance,
  @prev_client := t.client_id
  from(
    select * from stock order by client_id,date
  )t,(select @prev_client:=0,@cur_bal:=0,@tot_credit:=0,@tot_debit:= 0,@open_balance:=0)r
)s

<强> DEMO

另外我注意到你有相同的数据,我已经习惯了每个客户端ID进行排序,但最好有日期的日期时间,以便排序不会与同一日期混淆或者可能是表中的主键。