如何从第二列运行总计

时间:2015-05-21 13:15:59

标签: sql oracle lag window-functions analytic-functions

我有一个如下数据集,

Lot Size    Reported QTY    Qty Balance
150          100            
150          100            
150          80             
150          80            
150          5              

数量余额需要计算如下,

Row 1 = Lot Size - Reported Qty (row1) => 150-100 = 50
Row 2 = Reported Qty (row1) - Reported Qty(row2) => 100-100 =0
Row 3 = Reported Qty (row2) - Reported Qty(row3) => 100-80 =20
... till the last row

我的预期结果是

Lot Size    Reported QTY    Qty Balance
150          100            50
150          100            0
150          80             20
150          80             0
150          5              75

如何在查询中实现此目的?

3 个答案:

答案 0 :(得分:0)

您应该查看LAG()分析函数。有关函数及其接受的参数的详细信息,请参阅here(例如,我认为当lag()函数返回null时,您将需要传入lot_size列的默认值。)

一旦确定了上一行报告的数量值,就可以简单地减去它。当然,您需要一些方法来识别行的顺序 - 您的样本数据似乎没有一个,因此数据库将无法确定哪个行是第一行,哪个行是最后一行。

例如

with sample_data (lot_size, reported_qty) as (SELECT 150, 100 FROM DUAL
                                              UNION ALL SELECT 150, 100 FROM DUAL
                                              UNION ALL SELECT 150,  80 FROM DUAL
                                              UNION ALL SELECT 150,  80 FROM DUAL
                                              UNION ALL SELECT 150,   5 FROM DUAL)
select lot_size,
       reported_qty,
       lag(reported_qty, 1, lot_size) over (order by null) - reported_qty diff
from   sample_data;

  LOT_SIZE REPORTED_QTY       DIFF
---------- ------------ ----------
       150          100         50
       150          100          0
       150           80         20
       150           80          0
       150            5         75

答案 1 :(得分:0)

正如@Boneist建议的那样,你需要使用 LAG()OVER()分析函数。

你只需再做一个任务来处理第一行为NULL,使用 CASE 你可以使它工作。

测试用例

SQL> WITH data AS
  2    ( SELECT t.*, lag(reported_qty) OVER(ORDER BY NULL) rn FROM t
  3    )
  4  SELECT lot_size,
  5    reported_qty,
  6    CASE
  7      WHEN rn IS NULL
  8      THEN lot_size     - reported_qty
  9      ELSE rn - reported_qty
 10    END qty_balance
 11  FROM data;

  LOT_SIZE REPORTED_QTY QTY_BALANCE
---------- ------------ -----------
       150          100          50
       150          100           0
       150           80          20
       150           80           0
       150            5          75

SQL>

答案 2 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE lots ( Lot_Size, Reported_QTY ) AS
          SELECT 150, 100 FROM DUAL
UNION ALL SELECT 150, 100 FROM DUAL
UNION ALL SELECT 150,  80 FROM DUAL
UNION ALL SELECT 150,  80 FROM DUAL
UNION ALL SELECT 150,   5 FROM DUAL;

查询1

SELECT Lot_Size,
       Reported_QTY,
       COALESCE( LAG( Reported_QTY ) OVER ( ORDER BY NULL ) - Reported_QTY,
                 Lot_Size - Reported_QTY ) AS Qty_Balance
FROM   Lots

<强> Results

| LOT_SIZE | REPORTED_QTY | QTY_BALANCE |
|----------|--------------|-------------|
|      150 |          100 |          50 |
|      150 |          100 |           0 |
|      150 |           80 |          20 |
|      150 |           80 |           0 |
|      150 |            5 |          75 |