如果负的运行余额变为正运行余额,则使用sql情况确定

时间:2016-02-10 20:37:00

标签: sql case balance

请非常友好地指出我的SQL case select查询的正确方向:

我的案例规则目前如下:

When credit > running_balance then credit - running_balance

现在这就是我被困住的地方......如果上述规则为真,但running_balance再次变为正数,则应排除第一条规则,并将最后一笔信用作结果。

when Debit is not null and Debit <= 0 then 0
when Debit is not null and Debit > Running_Balance then Debit - Debit
when abs(Running_Balance) >= 0 and Credit > abs(Running_Balance) then 
case 
    when abs(Running_Balance) >= 0 and (abs(Running_Balance)*-1) < (abs(Running_Balance)*+1) then (Credit - abs(Running_Balance))
end

输出:

+----------------+---------+-----------------+-------------+----------+
|     Debit      | Credit  | Running_Balance | Ledger_Date |  Result  |
+----------------+---------+-----------------+-------------+----------+
| NULL           | 3606.57 | -2418.01        | 2015/11/16  | 1 188.56 |
| 3606.57        | NULL    | 1195.56         | 2015/11/16  | -        |
| NULL           | 5000    | 37475.48        | 2015/11/20  | 5 000.00 |
+----------------+---------+-----------------+-------------+----------+

期望的结果

+----------------+---------+-----------------+-------------+----------+
|     Debit      | Credit  | Running_Balance | Ledger_Date |  Result  |
+----------------+---------+-----------------+-------------+----------+
| NULL           | 3606.57 | -2418.01        | 2015/11/16  | -        |
| 3606.57        | NULL    | 1195.56         | 2015/11/16  | -        |
| NULL           | 5000    | 37475.48        | 2015/11/20  | 5 000.00 |
+----------------+---------+-----------------+-------------+----------+

谢天谢地

0 个答案:

没有答案