如何获得最后的运行余额

如何获得最后的运行余额

本文介绍了如何获得最后的运行余额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是查询...

+------+----------------+----+----+------------+-----+--------+------+
| seq  | idClient       | i  | WN | fld_Date   | PSF | BAL    | TBAL |
+------+----------------+----+----+------------+-----+--------+------+
| 1483 | SUB-01-1304002 |  1 | 11 | 2013-03-12 | 50  | 50.00  | 0.00 |
| 1486 | SUB-01-1304002 |  2 | 12 | 2013-03-19 | 25  | 75.00  | 0.00 |
| 1489 | SUB-01-1304002 |  3 | 13 | 2013-03-26 | 20  | 95.00  | 0.00 |
| 1492 | SUB-01-1304002 |  4 | 14 | 2013-04-02 | 20  | 115.00 | 0.00 |
| 1495 | SUB-01-1304002 |  5 | 15 | 2013-04-09 | 20  | 135.00 | 0.00 |
| 1498 | SUB-01-1304002 |  6 | 16 | 2013-04-16 | 20  | 155.00 | 0.00 |
| 1501 | SUB-01-1304002 |  7 | 17 | 2013-04-23 | 20  | 175.00 | 0.00 |
| 1504 | SUB-01-1304002 |  8 | 18 | 2013-04-30 | 20  | 195.00 | 0.00 |
| 1507 | SUB-01-1304002 |  9 | 19 | 2013-05-07 | 20  | 215.00 | 0.00 |
| 1510 | SUB-01-1304002 | 10 | 20 | 2013-05-14 | 20  | 235.00 | 0.00 |
| 1513 | SUB-01-1304002 | 11 | 21 | 2013-05-21 | 20  | 255.00 | 0.00 |
| 1516 | SUB-01-1304002 | 12 | 22 | 2013-05-28 | 20  | 275.00 | 0.00 |
| 1519 | SUB-01-1304002 | 13 | 23 | 2013-06-04 | 20  | 295.00 | 0.00 |
| 1522 | SUB-01-1304002 | 14 | 24 | 2013-06-11 | 20  | 315.00 | 0.00 |
| 1525 | SUB-01-1304002 | 15 | 25 | 2013-06-18 | -30 | 285.00 | 0.00 |
| 1548 | SUB-01-1304002 | 16 | 26 | 2013-06-25 | 20  | 305.00 | 0.00 |
+------+----------------+----+----+------------+-----+--------+------+

这是我想显示的输出

+------+----------------+----+----+------------+-----+--------+------+
| seq  | idClient       | i  | WN | fld_Date   | PSF | BAL    | TBAL |
+------+----------------+----+----+------------+-----+--------+------+
| 1548 | SUB-01-1304002 | 16 | 26 | 2013-06-25 | 20  | 305.00 | 0.00 |
+------+----------------+----+----+------------+-----+--------+------+

我已经使用了 max 但它没有用.任何人都可以帮助我解决这些问题.

I already use max but it didn't work. Anyone can help me with these.

这是我的代码

SELECT
MAX(seq) seq,
idClient,
BAL
FROM
(
SELECT
seq,
idClient,
IF (
@idClient = (@idClient := idClient),
@id :=@id + 1,
@id := 1
) i,
WEEKOFYEAR(fld_Date) WN,
fld_Date,
PSF,
IF(@id=1,@BAL:=PSF,@BAL:=@BAL+PSF) BAL,
FORMAT(@TBAL := COALESCE(@BAL*@DD,0),2) AS TBAL
FROM
(
    SELECT
        a.seq,
        a.idClient,
        a. fld_Date,
        a.PSF
    FROM
        (
SELECT
db_lms.lms_savings.seq,
db_lms.lms_savings.idClient,
db_lms.lms_savings.PSF,
db_lms.lms_savings.fld_Date
FROM
db_lms.lms_savings
WHERE db_lms.lms_savings.idClient = 'SUB-01-1304002'
            ORDER BY
                db_lms.lms_savings.idClient,
                db_lms.lms_savings.fld_Date
        ) a
    GROUP BY
        a.seq
    ORDER BY
        idClient,
        fld_Date
) a,
(SELECT @id := 0, @idClient := '', @BAL := 0) B
) z
GROUP BY idClient

推荐答案

尝试

SELECT z.*
  FROM
(
  SELECT seq,
         idClient,
         IF (
              @idClient = (@idClient := idClient),
              @id :=@id + 1,
              @id := 1
         ) i,
         WEEKOFYEAR(fld_Date) WN,
         fld_Date,
         PSF,
         IF(@id=1, @BAL:=PSF, @BAL := @BAL + PSF) BAL,
         FORMAT(@TBAL := COALESCE(@BAL * @DD, 0), 2) AS TBAL
  FROM
  (
      SELECT a.seq,
             a.idClient,
             a. fld_Date,
             a.PSF
        FROM
      (
        SELECT db_lms.lms_savings.seq,
               db_lms.lms_savings.idClient,
               db_lms.lms_savings.PSF,
               db_lms.lms_savings.fld_Date
          FROM db_lms.lms_savings
         WHERE db_lms.lms_savings.idClient = 'SUB-01-1304002'
         ORDER BY db_lms.lms_savings.idClient,
                 db_lms.lms_savings.fld_Date
      ) a
       GROUP BY a.seq
       ORDER BY idClient,
                fld_Date
  ) a,
  (SELECT @id := 0, @idClient := '', @BAL := 0) b
) z
 ORDER BY seq DESC
 LIMIT 1;

这篇关于如何获得最后的运行余额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:34