Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 关键字';超过';_Sql Server 2005_Syntax - Fatal编程技术网

Sql server 2005 关键字';超过';

Sql server 2005 关键字';超过';,sql-server-2005,syntax,Sql Server 2005,Syntax,当试图计算表达式时,在关键字“OVER”附近出现错误语法错误。 我不确定这行的错误是什么: (SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost] )) - ((@PurchaseCost + @Prod_CostLBS ) * @InputWeight ) OVER (PARTITION BY [ARC].[CustomerCode] ) 我可以在另一个表达式上使用OVER,它工作得很好,格式与上面的一样,只是只取了一列的SUM 完整代码: SET NOCO

当试图计算表达式时,在关键字“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)))
语法不正确

您不能在聚合和
之间粘贴任意表达式

大概你需要

SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost]) 
OVER (PARTITION BY [ARC].[CustomerCode]) 
- ( ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight ) AS [Profit]
如果您试图在
总和之后调整结果


如果您试图调整求和的值。

求和函数不会一直到达OVER表达式。你是不是在求和之后和结束之前缺少了一组括号?@Sako73你是对的,这消除了错误,但也打乱了我的计算,这就是为什么我一开始就有这样的括号。我想说,你提供的例子与我一直试图完成的最接近。但是,我仍然得到了错误的值。我试着在购买成本之前加上总和,因为我需要整个等式的总和。有什么想法吗?如果我不清楚,我需要数量库存和单位成本之和,然后我需要用采购成本、生产成本和输入权重之和减去。我在问题中添加了示例数据。
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost]) 
    - ((@PurchaseCost + @Prod_CostLBS) * @InputWeight) 
   OVER (PARTITION BY [ARC].[CustomerCode]) AS [Profit]
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost]) 
OVER (PARTITION BY [ARC].[CustomerCode]) 
- ( ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight ) AS [Profit]
SUM([DCT].[Quantity_Stk] * [ICP].[UnitCost] 
- ( ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight )) 
OVER (PARTITION BY [ARC].[CustomerCode]) AS [Profit]