-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