John John - 5 months ago 10
MySQL Question

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 "ASC" 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 "ASC" to "DESC" the balance will become the amount of the last transaction. like debit=30,000, balance=30,000 also.

"DESC" 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

Answer

After having a discussion I finally came with this:

SELECT 
*
FROM 
(
    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 ASC, s.TR_CODE, s.TR_CTLNO 
    ) AS t , (SELECT @balance := 0) var
) firstTableOutput
ORDER BY firstTableOutput.TR_DATE DESC;

Note:

  • GROUPING BY s.TR_DATE in ascending order.
  • Then make an alias of the table.
  • And finally sort the alias table's output in descending order.
Comments