0

这是查询...

+------+----------------+----+----+------------+-----+--------+------+
| 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,但它没有用。任何人都可以帮助我解决这些问题。

这是我的代码

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
4

2 回答 2

0

这是你需要的吗?

SELECT
    *
FROM
    lms_savings
WHERE
    seq = 
    (
        SELECT
            seq
        FROM
            lms_savings
        ORDER BY
            fld_Date DESC
        LIMIT 1
    )
于 2013-08-02T06:27:31.697 回答
0

尝试

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;
于 2013-08-02T07:12:22.097 回答