尝试计算表达式时,出现错误Incorrect syntax near the keyword 'OVER'.
我不确定这一行的错误是什么:
(SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost] )) - ((@PurchaseCost + @Prod_CostLBS ) * @InputWeight ) OVER (PARTITION BY [ARC].[CustomerCode] )
我能够在另一个表达式上使用OVER
,它工作得很好,格式与上面一样,只是它只占用了一列的SUM
。
完整代码:
SET NOCOUNT ON;
DECLARE @PurchaseCost Decimal(19,8);
DECLARE @InputWeight Decimal(19,8);
DECLARE @Prod_CostLBS Decimal(19,8);
SET @PurchaseCost = 2.58;
SET @InputWeight = 18100;
SET @Prod_CostLBS = .15;
SELECT
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([DCT].[Quantity_Stk] *[ICP].[UnitCost]) AS [Total Sales]
, Avg(([IC_ProductLots].[OriginalQuantity_Stk] / [IC_ProductLots].[OriginalQuantity_Alt])) AS [Avg. Box Weight]
, Sum([IC_ProductLots].[OriginalQuantity_Stk] / @InputWeight) AS [Yield]
, CAST (@InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER () AS DECIMAL(18,2)) AS [Shrink]
, Max(CAST ((@PurchaseCost + @Prod_CostLBS) * @InputWeight AS DECIMAL (18,2))) AS [Cost]
, SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost]) - ((@PurchaseCost + @Prod_CostLBS) * @InputWeight) OVER (PARTITION BY [ARC].[CustomerCode])
AS [Profit]
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-24 00:00:00' } AND (IC_ProductLots.ProductionDate <= { ts '2015-06-24 00:00:00' } OR IC_ProductLots.ProductionDate Is Null))
AND ((1=1) AND [ARC].CustomerKey IN (124) )
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].CustomerKey
ORDER BY
CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40))
, CAST (@InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER () AS DECIMAL(18,2))
, Max(CAST ((@PurchaseCost + @Prod_CostLBS) * @InputWeight AS DECIMAL (18,2)))
我使用的是Microsoft SQL Server 2005。
示例数据:
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost]
- ( ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight ))
OVER (PARTITION BY [ARC].[CustomerCode]) AS [Profit]
DCT.Quanity_Stk = 25
ICP.UnitCost = 3.50
PurchaseCost = 2.50
Prod_CostLBS = .50
InputWeight = 30
(25 * 3.50) - ((2.50 + .50) * 30) = -2.5
87.5 - (3 * 30) = -2.5
87.5 - 90 = -2.5
答案 0 :(得分:2)
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost])
- ((@PurchaseCost + @Prod_CostLBS) * @InputWeight)
OVER (PARTITION BY [ARC].[CustomerCode]) AS [Profit]
语法不正确。
您不能在聚合与over
大概你需要
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost])
OVER (PARTITION BY [ARC].[CustomerCode])
- ( ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight ) AS [Profit]
如果您尝试在SUM
之后调整结果。
或
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost]
- ( ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight ))
OVER (PARTITION BY [ARC].[CustomerCode]) AS [Profit]
如果您尝试调整得到的值。