在第二列中运行总计 - 跟进

时间:2015-05-22 14:07:35

标签: oracle

这是我之前question的后续行动。

我现在拥有的数据就像这样

PART_NO  ORDER_NO   OPERATION_NO LOT_SIZE   REPORTED_QTY    QTY_BALANCE
305002   10374      10           3000       2417            
305002   10374      11           3000       2417            
305002   10374      12           3000       2417            
305002   10374      13           3000       2417            
305002   10374      14           3000       2417            
305002   10374      15           3000       2417            
305002   9145       10           2000       1950            
305002   9145       20           2000       1949            
305002   9145       30           2000       1949            
305002   9145       40           2000       1949            
305002   9145       50           2000       1949            
305002   9145       60           2000       1949            

当我使用LAG功能时,我得到如下结果,

PART_NO  ORDER_NO   OPERATION_NO LOT_SIZE   REPORTED_QTY    QTY_BALANCE
305002   10374      10           3000       2417            583
305002   10374      11           3000       2417            0
305002   10374      12           3000       2417            0
305002   10374      13           3000       2417            0
305002   10374      14           3000       2417            0
305002   10374      15           3000       2417            0
305002   9145       10           2000       1950            467
305002   9145       20           2000       1949            1
305002   9145       30           2000       1949            0
305002   9145       40           2000       1949            0
305002   9145       50           2000       1949            0
305002   9145       60           2000       1949            0

问题是,当order_no发生变化时,我需要从该order_no的开头开始LAG评估。预期的结果集是

PART_NO  ORDER_NO   OPERATION_NO LOT_SIZE   REPORTED_QTY    QTY_BALANCE
305002   10374      10           3000       2417            583
305002   10374      11           3000       2417            0
305002   10374      12           3000       2417            0
305002   10374      13           3000       2417            0
305002   10374      14           3000       2417            0
305002   10374      15           3000       2417            0
305002   9145       10           2000       1950            50
305002   9145       20           2000       1949            1
305002   9145       30           2000       1949            0
305002   9145       40           2000       1949            0
305002   9145       50           2000       1949            0
305002   9145       60           2000       1949            0

我似乎无法解决这个问题。如何使LAG函数理解只要order_no发生变化就需要重置?

1 个答案:

答案 0 :(得分:0)

PARTITION BY order_no声明中使用OVER子句:

SELECT PART_NO, ORDER_NO, OPERATION_NO, Lot_Size,
       Reported_QTY,
       COALESCE( LAG( Reported_QTY ) OVER (PARTITION BY ORDER_NO ORDER BY null )
                - Reported_QTY, Lot_Size - Reported_QTY ) AS Qty_Balance
FROM   Lots
ORDER BY ORDER_NO DESC, OPERATION_NO

演示:http://sqlfiddle.com/#!4/501c2/15