【问题标题】:How to get the New Running Balance from Existing Balance?如何从现有余额中获取新的运行余额?
【发布时间】:2014-07-02 04:52:58
【问题描述】:

这是我的 sql 和我的查询输出...

sql:

SELECT
id ID, 
token TK, 
actual_pay PAY,
IF(@rtp IS NULL, @rtp:=token, @rtp:=@bal+actual_pay) RTP,
IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) BAL
FROM token_table a
JOIN (SELECT @rtp:=NULL, @bal:=NULL) b;

输出:

+----+------+-----+------+------+
| ID | TK   | PAY | RTP  | BAL  |
+----+------+-----+------+------+
|  1 | 500  | 900 |  500 |  400 |
|  2 | 1200 | 900 | 1300 |  100 |
|  3 | 900  | 900 | 1000 |  100 |
|  4 | 900  | 900 | 1000 |  100 |
|  5 | 400  | 900 | 1000 |  600 |
|  6 | 300  | 900 | 1500 | 1200 |
|  7 | 500  | 900 | 2100 | 1600 |
|  8 | 1700 | 900 | 2500 |  800 |
|  9 | 1800 | 900 | 1700 | -100 |
| 10 | 800  | 900 |  800 |    0 |
| 11 | 900  | 900 |  900 |    0 |
| 12 | 0    | 850 |  850 |  850 |
+----+------+-----+------+------+

这是我想要得到的输出......

问题:
1. stat字段的公式为:如果BAL(从ID=1)的值小于或等于TK(从ID=2)的值,如果是则该值应为1,否则该值应为0 .

2、nbal字段的公式为:如果BAL(从ID=1)的值小于或等于TK(从ID=2)的值,如果是则该值应为0,否则该值应为BAL (从 ID=1)减去 TK(从 ID=2)。

3. ntk字段的公式为:如果BAL(从ID=1)的值小于或等于TK(从ID=2)的值,如果是,则该值应该是TK(从ID=2)减去BAL (来自 ID=1),否则该值应为 BAL(来自 ID=1)减去 TK(来自 ID=2)。

+----+------+-----+------+------+------+------+------+
| ID | TK   | PAY | RTP  | BAL  | stat | nbal | ntk  |
+----+------+-----+------+------+------+------+------+
|  1 | 500  | 900 |  500 |  400 |    1 | 0    | 0    |
|  2 | 1200 | 900 | 1300 |  100 |    1 | 0    | 800  |
|  3 | 900  | 900 | 1000 |  100 |    1 | 0    | 800  |
|  4 | 900  | 900 | 1000 |  100 |    1 | 0    | 800  |
|  5 | 400  | 900 | 1000 |  600 |    0 | 300  | 300  |
|  6 | 300  | 900 | 1500 | 1200 |    0 | 700  | 0    |
|  7 | 500  | 900 | 2100 | 1600 |    1 | 0    | 0    |
|  8 | 1700 | 900 | 2500 |  800 |    1 | 0    | 100  |
|  9 | 1800 | 900 | 1700 | -100 |    1 | 0    | 1000 |
| 10 | 800  | 900 |  800 |    0 |    1 | 0    | 900  |
| 11 | 900  | 900 |  900 |    0 |    1 | 0    | 900  |
| 12 | 0    | 850 |  850 |  850 |    0 | 850  | 0    |
+----+------+-----+------+------+------+------+------+

【问题讨论】:

    标签: mysql sql balance


    【解决方案1】:

    Case statement 可以处理您的情况。

    SELECT id ID, token TK, actual_pay PAY,
           IF(@rtp IS NULL, @rtp:=token, @rtp:=@bal+actual_pay) RTP,
           IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) BAL,
    
           (case IF(@rtp IS NULL, @rtp:=token, @rtp:=@bal+actual_pay)
             when IF(@rtp IS NULL, @rtp:=token, @rtp:=@bal+actual_pay) <= 
                  (select token from token_table where id = a.id+1)
             then 1
            else 0
           end case) stat,
    
          (case IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token)
            when IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) <= 
                 (select token from token_table where id = a.id+1)
            then 0
           else 
            IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) - 
            (select token from token_table where id = a.id+1)
          end case) nbal,
    
          (case IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token)
            when IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) <= 
                 (select token from token_table where id = a.id+1)           
            then
                (select token from token_table where id = a.id+1)  -
                IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token)
           else 
            IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) - 
            (select token from token_table where id = a.id+1)
          end case) ntk
    
    FROM token_table a
    JOIN (SELECT @rtp:=NULL, @bal:=NULL) b;
    

    【讨论】:

      【解决方案2】:

      left join 添加到下一行:

      select a.id, a.tk, a.pay, a.rtp, a.bal, a.stat, a.nbal, a.ntk
      from (
        select a.id, a.token tk, a.actual_pay pay,
          if(@rtp is null, @rtp:=a.token, @rtp:=@bal+a.actual_pay) rtp,
          ifnull(abs(@bal-a.token),0) ntk,
          if(@bal is null, @bal:=a.actual_pay-a.token, @bal:=@rtp-a.token) bal,
          @bal <= ifnull(c.token,0) stat,
          greatest(0, @bal-ifnull(c.token,0)) nbal
        from records a
        join (select @rtp:=null, @bal:=null) b
        left join records c on a.id = c.id-1) a;
      

      fiddle

      【讨论】:

      • 感谢您的出色回答。一件事,ntk(from ID=1)的值应该是0,ntk(from ID=12)的值也应该是0。
      • @xenonleaux,我知道如果你想将最后一个设置为零,因为没有第 13 行,但第 1 行不应该是1200 - 400
      • dl.dropboxusercontent.com/u/51673150/problem.png 这是我尝试在 excel 中修复的示例图像....
      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多