How to get the New Running Balance from Existing Balance?

微笑、不失礼 提交于 2019-12-11 09:40:57

问题


Here's my sql and Output of my query...

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;

Output:

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

Here's the output that I want to get...

Problem:
1. The formula of stat field is: If value of BAL(from ID=1) is less than or equal to value of TK(from ID=2), if yes the value should be 1, if else the value should be 0.

2. The formula of nbal field is:If value of BAL(from ID=1) is less than or equal to value of TK(from ID=2), if yes the value should be 0, if else the value should be BAL(from ID=1) minus TK(from ID=2).

3. The formula of ntk field is: If value of BAL(from ID=1) is less than or equal to value of TK(from ID=2), if yes the value should be TK(from ID=2) minus BAL(from ID=1), if else the value should be BAL(from ID=1) minus TK(from 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    |
+----+------+-----+------+------+------+------+------+

回答1:


A Case statement can handle your condition.

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:


Adding left join onto the next row:

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



来源:https://stackoverflow.com/questions/24523109/how-to-get-the-new-running-balance-from-existing-balance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!