计算的聚合函数错误
Aggregate function error for calculation
对于我正在创建的报告,我需要计算出 SUM 并将其用于另一个方程式。
该方程用于计算 Shrink,这与差异是一回事。
要计算收缩率,我使用:
@InputWeight - Sum([IC_ProductLots].[OriginalQuantity_Stk]) AS [Shrink]
输入权重由用户在运行报告前给出。
因此,例如,如果输入重量为 15,000,而原始数量的 SUM 为 14,800,则 Shrink 应为 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]
但我收到错误:"Column 'IC_ProductLots.OriginalQuantity_Stk' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
我觉得你想要的表达方式是:
@InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER ()
注意:您不需要将 select distinct
与 group by
一起使用,因此您应该删除不同的。
对于我正在创建的报告,我需要计算出 SUM 并将其用于另一个方程式。 该方程用于计算 Shrink,这与差异是一回事。 要计算收缩率,我使用:
@InputWeight - Sum([IC_ProductLots].[OriginalQuantity_Stk]) AS [Shrink]
输入权重由用户在运行报告前给出。 因此,例如,如果输入重量为 15,000,而原始数量的 SUM 为 14,800,则 Shrink 应为 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]
但我收到错误:"Column 'IC_ProductLots.OriginalQuantity_Stk' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
我觉得你想要的表达方式是:
@InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER ()
注意:您不需要将 select distinct
与 group by
一起使用,因此您应该删除不同的。