-2
需要關於如何在我的數據庫中的交易表中獲得期初餘額和期末餘額的幫助。非工作日的期初餘額和期末餘額
事務表
+------------------+----------+-----------+
| id | trans_date | debit | credit |
+----+-------------+----------+-----------+
| 1 | 2016-05-09 | 200.00 | 0.00 |
| 2 | 2016-05-11 | 0.00 | 50.00 |
+---------------+-------------+-----------+
要像下面的結果。您會意識到「2016-05-10」沒有交易,但結果顯示期初餘額和期末餘額。謝謝
+-------------+--------------+-----------+-----------+------------+
| trans_date | open_bal | debit | credit |closing_bal |
+-------------+--------------+-----------+-----------+------------+
| 2016-05-09 | 0.00 | 200.00 | 0.00 | 200.00 |
| 2016-05-10 | 200.00 | 0.00 | 0.00 | 200.00 |
| 2016-05-11 | 200.00 | 0.00 | 50.00 | 150.00 |
+-------------+-------------+--------------+------------+---------+
我試過這個,但「2016-05-10」沒有顯示在結果中。
SELECT trans_date,open_balance
FROM(SELECT s.gen_id, s.trans_id, s.trans_date,
s.narations, s.account_code,
s.op_balance as open_balance,
s.debit, s.credit, s.closing_balance
from (select t.gen_id, t.trans_id,
t.narations, t.account_code,
t.trans_date, t.credit, t.debit,
@tot_debit := if(@prev_client = t.account_code, @tot_debit + t.debit,t.debit) as tot_cred,
@tot_credit := if(@prev_client = t.account_code,@tot_credit + t.credit,t.credit) as tot_deb,
@cur_bal := if(@prev_client = t.account_code, @tot_debit - @tot_credit,t.debit-t.credit) as closing_balance,
(@cur_bal + t.credit) - t.debit as op_balance, @prev_client := t.account_code
from (select * from journal WHERE account_code = 41003
GROUP BY trans_date order by trans_date,account_code,trans_id)t, (select @prev_client:=0,@cur_bal:=0,@tot_debit:=0,@tot_credit:= 0,@open_balance:=0)r)s) g where trans_date <= '2016-05-11'
不客氣。至少在你問之前試一下。 – user3791372
只需處理應用程序級代碼中缺少日期的邏輯即可 – Strawberry