来自先前 ROW 值的 SUM

SUM from previous ROW values

如何将先前行的值和当前行的值相加到一个新列中? 来自这个查询:

SELECT          
    A.DATE,     
    A.SHIFT,        
    A.RECEIPT,      
    A.ISSUE     
FROM            
    (SELECT     
        B.DATE, 
        B.SHIFT,    
        CASE WHEN B.JUDUL='ISSUE' THEN B.QTY END AS ISSUE,  
        CASE WHEN B.JUDUL='RECEIPT' THEN B.QTY END AS RECEIPT   
    FROM        
        (SELECT 
            T1.DATE AS DATE,
            T1.SHIFT AS SHIFT
            SUM(T1.QTY) AS QTY,
            'ISSUE' AS JUDUL
        FROM    
            ISSUE T1
        WHERE   
            T1.PO=[%0]
        GROUP BY    
            T1.DATE
        UNION   
        SELECT  
            T2.DATE AS DATE,
            T2.SHIFT AS SHIFT,
            SUM(T2.QTY) AS QTY,
            'RECEIPT' AS JUDUL
        FROM    
            RECEIPT T2
        WHERE   
            T2.PO=[%0]
        GROUP BY    
            T2.DATE) B) A

我得到了这样的结果:

DATE    SHIFT   RECEIPT ISSUE
02.01.18    1   39.20   204.20
02.01.18    2   112.95  145.60
02.01.18    3   125.46  0.00
03.01.18    1   131.72  145.60
03.01.18    2   132.79  145.60
03.01.18    3   118.28  72.85
04.01.18    1   122.04  145.60
04.01.18    2   155.08  72.80
04.01.18    3   116.98  72.80
05.01.18    1   91.69   145.60

但实际上我需要这样的结果:

DATE    SHIFT   RECEIPT ISSUE   TotalReceipt    TotalIssue  VAR
02.01.18    1   39.20   204.20  39.20           204.20      165.00
02.01.18    2   112.95  145.60  152.15          349.80      197.65
02.01.18    3   125.46  0.00    277.61          349.80      72.19
03.01.18    1   131.72  145.60  409.33          495.40      86.07
03.01.18    2   132.79  145.60  542.12          641.00      98.88
03.01.18    3   118.28  72.85   660.40          713.85      53.45
04.01.18    1   122.04  145.60  782.44          859.45      77.01
04.01.18    2   155.08  72.80   937.52          932.25      -5.27
04.01.18    3   116.98  72.80   1,054.50        1,005.05    -49.45
05.01.18    1   91.69   145.60  1,146.19        1,150.65    4.46

我需要前一天的数据来比较发货和收货交易之间的数据。

可以使用累加函数(ANSI SQL):

with t as (
      <your query here>
     )
select t.*,
       sum(receipt) over (order by date, shift) as totalreceipt,
       sum(issue) over (order by date, shift) as totalissue,
       sum(issue - receipt) over (order by date, shift) as variance
from t;

使用Window Function,你可以做到这一点。

试试这个:

SELECT          
    A.DATE,     
    A.SHIFT,        
    A.RECEIPT,      
    A.ISSUE,
    SUM(A.RECEIPT) OVER (ORDER BY A.DATE, A.SHIFT) AS TotalReceipt,
    SUM(A.ISSUE) OVER (ORDER BY A.DATE, A.SHIFT) AS TotalIssue,
    SUM(A.ISSUE - A.RECEIPT) OVER (ORDER BY A.DATE, A.SHIFT) AS VAR
FROM            
    (SELECT     
        B.DATE, 
        B.SHIFT,    
        CASE WHEN B.JUDUL='ISSUE' THEN B.QTY END AS ISSUE,  
        CASE WHEN B.JUDUL='RECEIPT' THEN B.QTY END AS RECEIPT   
    FROM        
        (SELECT 
            T1.DATE AS DATE,
            T1.SHIFT AS SHIFT
            SUM(T1.QTY) AS QTY,
            'ISSUE' AS JUDUL
        FROM    
            ISSUE T1
        WHERE   
            T1.PO=[%0]
        GROUP BY    
            T1.DATE
        UNION   
        SELECT  
            T2.DATE AS DATE,
            T2.SHIFT AS SHIFT,
            SUM(T2.QTY) AS QTY,
            'RECEIPT' AS JUDUL
        FROM    
            RECEIPT T2
        WHERE   
            T2.PO=[%0]
        GROUP BY    
            T2.DATE) B) A