非工作日期的期初余额和期末余额

时间:2016-05-16 03:02:35

标签: mysql

需要有关如何在我的数据库中的交易表中获取期初余额和非工作日期余额的帮助。

交易表

+------------------+----------+-----------+
| 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'

1 个答案:

答案 0 :(得分:0)

您可以使用它来选择MySQL中的日期列表。

SELECT DATE_ADD(CURDATE() , INTERVAL -(a.n + 10 * b.n + 100 * c.n) DAY) AS `date`
FROM (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS a
  CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS b
  CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS c

如果您想要包含在表transactions中未显示的结果日期,可以使用LEFT JOIN

来完成
SELECT * FROM
  (SELECT DATE_ADD(CURDATE() , INTERVAL -(a.n + 10 * b.n + 100 * c.n) DAY) AS `date`
    FROM (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS a
      CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS b
      CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS c
  ) AS dates
  LEFT JOIN transactions ON trans_date = dates.date

请注意,这将选择过去999天的当前日期。如果您想要更多天前,可以添加另一个CROSS JOIN,并返回9 999天。