如何更正每笔交易的“余额”。按日期分组DESC

时间:2016-06-23 02:34:48

标签: mysql

美好的一天,我需要你们的帮助,这样我就可以在“BALANCE”栏中为交易日期的“降序”顺序中的每笔交易生成正确的数据。

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;  

上面的查询结果与交易日期的“ASC”顺序

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

当我将“ASC”替换为“DESC”时,余额将成为最后一笔交易的金额。比如借记= 30,000,余额= 30,000。

“DESC”交易日期的顺序。需要的结果。

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

使用TR_DATE编辑

2 个答案:

答案 0 :(得分:1)

在讨论之后我终于来了:

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;

注意:

  • GROUPING BY s.TR_DATE 升序顺序。
  • 然后制作表格的别名
  • 最后以降序顺序对别名表的输出进行排序。

答案 1 :(得分:0)

将整个查询用作子查询,并按tr_date desc排序。我已修改了查询,但您提供的查询似乎不完整。我没有在您的选择查询中看到选择日期列。

 select A.* from (

  SELECT 
  t.tr_date
  t.DEBIT,
  t.CREDIT,
  @balance := @balance + t.DEBIT - t.CREDIT AS BALANCE
  FROM 
  (
    SELECT 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;  

 ) A order by A.tr_date desc