来自先前 ROW 值的 SUM

Posted

技术标签:

【中文标题】来自先前 ROW 值的 SUM【英文标题】:SUM from previous ROW values 【发布时间】:2018-01-19 01:58:46 【问题描述】:

如何将前行值和当前行值相加到新列? 从这个查询:

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

我需要前一个班次日期的数据来比较签发和收据交易之间的数据。

【问题讨论】:

请指定您使用的数据库系统。答案可能不同。 我用的是SAP B1 v9.2,不知道数据库系统是什么。 【参考方案1】:

可以使用累加函数(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;

【讨论】:

对不起,我有这样的错误:1)。 [Microsoft][SQL Server Native Client 10.0][SQL Server]ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP 或 FOR XML。 2)。 [Microsoft][SQL Server Native Client 10.0][SQL Server]'order' 附近的语法不正确。 @IpanUtomo 根据错误消息,我为您的 RDBMS 问题添加了适当的标签。【参考方案2】:

使用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

【讨论】:

我有这样的错误:1)。 [Microsoft][SQL Server Native Client 10.0][SQL Server]'order' 附近的语法不正确。 @IpanUtomo 这个window function 仅适用于 SQL Server,您使用的是哪个服务器SQL server、mysql 或 Oracle...

以上是关于来自先前 ROW 值的 SUM的主要内容,如果未能解决你的问题,请参考以下文章

具有先前值的 Python Pandas iterrows()

Mongoose 是不是提供对 pre('save') 中属性先前值的访问?

石墨:显示与先前值的变化

获取所有先前值的总和? - 到目前为止的总数? [复制]

如何从 uibutton 中获取先前关于标签值的图像和标题?

计算按日期范围拆分的值的先前出现次数