用于计算的聚合函数错误

时间:2015-07-13 16:48:42

标签: sql sql-server-2005 aggregate-functions running-total

对于我正在创建的报告,我需要采用计算的SUM并将其用于另一个等式。 该等式用于计算收缩,这与差异相同。 要计算收缩我使用:

@InputWeight - Sum([IC_ProductLots].[OriginalQuantity_Stk]) AS [Shrink]

输入权重由用户在运行报告之前给出。 因此,例如,如果输入权重为15,000且原始数量的总和为14,800,则收缩应为200。

问题是该代码正在计算每行的收缩,每个报告应该只有一个收缩。

示例输出如下所示:

Input Weight: 15,000
Product # | Weight | Shrink
1234      | 7,000  | 
1235      | 6,500  | 
1236      | 500    | 
1237      | 800    | 
--------------------------
TOTAL     |14,800  | 200 

我使用的是Microsoft SQL Server 2005。

以下是我的完整代码,如果有帮助:

SET NOCOUNT ON; 
DECLARE @PurchaseCost Decimal(19,8);
DECLARE @InputWeight Decimal(19,8);

SET @PurchaseCost = 1;
SET @InputWeight = 15000;
    SELECT DISTINCT 
         CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40)) AS [Supplier]
       , [PC].ProductCode
       , [PC].Description1
       , Count(IC_ProductLots.OriginalQuantity_Alt) AS [Boxes]
       , IC_ProductLots.UnitOfMeasure_Alt
       , Sum(IC_ProductLots.OriginalQuantity_Stk) AS [Weight]
       , IC_ProductLots.UnitOfMeasure_Stk
       , [ICP].UnitCost AS [Unit Cost]
       , Sum(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2)) AS [Total Sales]
       , Avg(([IC_ProductLots].[OriginalQuantity_Stk] / [IC_ProductLots].[OriginalQuantity_Alt])) AS [Avg. Box Weight]
       , Sum([IC_ProductLots].[OriginalQuantity_Stk] / @InputWeight) AS [Yield]
       , @InputWeight - Sum([IC_ProductLots].[OriginalQuantity_Stk]) AS [Shrink]
     FROM (((( IC_Products [PC] 
        INNER JOIN  DC_Transactions [DCT] 
         ON [PC].ProductKey = [DCT].ProductKey)
        INNER JOIN  AR_Customers [ARC] 
         ON [DCT].CustomerKey = [ARC].CustomerKey)
        INNER JOIN  IC_ProductLots 
         ON [DCT].LotKey = IC_ProductLots.LotKey)
        LEFT OUTER JOIN  IC_ProductCosts [ICP] 
         ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5)
     WHERE 
        (IC_ProductLots.ProductionDate >= { ts '2015-06-29 00:00:00' }   AND (IC_ProductLots.ProductionDate <= { ts '2015-06-29 00:00:00' } OR IC_ProductLots.ProductionDate Is Null)) 
    AND ([ARC].CustomerCode = '         904') 
     GROUP BY 
         CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40))
       , [PC].ProductCode
       , [PC].Description1
       , IC_ProductLots.UnitOfMeasure_Alt
       , IC_ProductLots.UnitOfMeasure_Stk
       , [ICP].UnitCost
       , IC_ProductLots.ProductionDate
       , [ARC].CustomerCode
     ORDER BY 
         CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40))

基本上,我需要计算每个行的总收缩而不是收缩。

更新

我尝试使用

@InputWeight - Sum([IC_ProductLots].[OriginalQuantity_Stk]) OVER(PARTITION BY [ARC].[CustomerCode]) AS [Shrink]

但我收到错误:“列'IC_ProductLots.OriginalQuantity_Stk'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。”

1 个答案:

答案 0 :(得分:1)

我认为你想要的表达是:

@InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER ()

注意:您不需要将select distinctgroup by一起使用,因此您应该删除不同的内容。