2015-03-03 101 views
0

我收到了一張名爲「Stock」的表,如下所示。MySQL未結餘額貸方餘額餘額

+-----------+--------------+---------------+---------+ 
| 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和日期順序來計算,如上圖所示。請幫忙。

+3

先試試 – 2015-03-03 09:26:41

+0

你是怎麼計算'Open balances''? – Saif 2015-03-03 09:37:36

+0

每個client_id的初始'未結餘額'爲零。 – 2015-03-03 09:44:36

回答

1

在這裏你可以怎麼做呢..

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排序日期列,但它有datetime日期以便排序不會與相同日期混淆或可能是表中的主鍵。

+0

謝謝Abhik Chakraborty,它完美的作品。 – 2015-03-05 11:07:30

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([email protected],@balance:[email protected],@balance:=0), 
@openBalance:[email protected] as OpenBalance,credit,debit,@balance:=([email protected])-debit as 
bal,@id:=client_id from Stock order by client_id,date; 

是啊,因爲有些數據具有相同的日期和編號,查詢可能工作方式不同所以請做一些修改在你的表格定義中,比如日期而不是日期,你可以設定日期時間,然後進行相應的排序。