jeudi 23 juin 2016
How to correct the "Balance" per transaction. Group by Date ASC
Good day, I need your help guys so that I can generate the correct data in "BALANCE" column for every transaction in "ASCENDING" order of the transaction date.
SELECT
t.TR_DATE
t.DEBIT,
t.CREDIT,
@balance := @balance + t.DEBIT - t.CREDIT AS BALANCE
FROM
(
SELECT s.TR_DATE AS TR_DATE,
SUM(IF(s.AMT>0 AND s.SLE_CODE=11,s.AMT,0)) AS DEBIT,
SUM(IF(s.AMT<0 AND s.SLE_CODE=11,s.AMT,0)) * -1 AS CREDIT
FROM
sldtl AS s
LEFT JOIN
transtype
ON
transtype.TransTypeID = s.TR_CODE
WHERE
s.SL_BRCODE= 1 AND s.SL_CLIENTID= 267 AND s.SLC_CODE= 13 AND s.SLT_CODE= 15 AND
s.REF_NO= s.REF_NO AND s.TR_DATE <= CURDATE()
GROUP BY
s.TR_DATE DESC, s.TR_CODE, s.TR_CTLNO
) AS t , (SELECT @balance := 0) var;
Result of the query above with "DESC" order of the transaction date
Accounts Payable
**Note in this Accounts Debit is a deduction.
Date of Deposit: 2011-12-31
Initial amount 79,799.44 as of 2011-12-31
+------------+-------------+---------------+-------------+
| Trans Date | Debit | Credit | Balance |
+------------+-------------+---------------+-------------+
| 2011-12-31 | 0.00 | 79,799.44 | 79,799.44 |->First Trans date
| 2012-01-15 | 0.00 | 560.27 | 80,359.71 |
| 2012-01-31 | 0.00 | 500.00 | 80,859.71 |
| .......... | ........ | ........... | ........... |
| .......... | ........ | ........... | ........... |
| .......... | ........ | ........... | ........... |
| .......... | ........ | ........... | ........... |
| 2016-03-15 | 0.00 | 1,000.00 | 92,218.37 |
| 2016-03-30 | 0.00 | 1,000.00 | 93,218.37 |
| 2016-04-06 | 30,000.00 | 0.00 | 63,218.37 |->Last Trans date
+------------+-------------+---------------+-------------+
When I replace the "DESC" to "ASC" the balance will become the amount of the last transaction. like debit=30,000, balance=30,000 also.
"ASC" order of transaction date. Needed Result.
Accounts Payable
**Note in this Accounts Debit is a deduction.
Date of Deposit: 2011-12-31
Initial amount 79,799.44 as of 2011-12-31
+------------+-------------+---------------+-------------+
| Trans Date | Debit | Credit | Balance |
+------------+-------------+---------------+-------------+
| 2016-04-06 | 30,000.00 | 0.00 | 63,218.37 |->Last Trans date
| 2016-03-30 | 0.00 | 1,000.00 | 93,218.37 |
| 2016-03-15 | 0.00 | 1,000.00 | 92,218.37 |
| .......... | ........ | ........... | ........... |
| .......... | ........ | ........... | ........... |
| .......... | ........ | ........... | ........... |
| .......... | ........ | ........... | ........... |
| 2012-01-31 | 0.00 | 500.00 | 80,859.71 |
| 2012-01-15 | 0.00 | 560.27 | 80,359.71 |
| 2011-12-31 | 0.00 | 79,799.44 | 79,799.44 |->First Trans date
+------------+-------------+---------------+-------------+
Edited with TR_DATE
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire