计算结合两个方程的SQL错误

时间:2015-07-29 13:44:30

标签: sql-server sum

我需要找到一组产品的总价值。但是,我的报告仍然需要显示其他产品的价值。我需要的产品的产品代码总价值是5780,一切都不到5700。

要计算我使用的总值,但这不再提供必要的值,因为@Credits不是正确的值:

 SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits) END ) OVER()

这很有效,直到我意识到我无法除以@Credits,我需要除以产品代码5780的产品重量的SUM以及更少的东西比5700。

要计算产品代码5780的SUM以及我使用的少于5700的所有内容:

SUM(SUM(PL.OriginalQuantity_Stk)) OVER
       (PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
                           THEN PC.ProductCategoryCode
                      END)
       ) 

我尝试使用以下公式:

SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else ((ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk))) /SUM(Sum(PL.OriginalQuantity_Stk) )) END ) OVER()

或者:

 SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END )) OVER()
) END ) OVER()

但是我得到了错误:

  

无法对包含的表达式执行聚合函数   聚合或子查询。

示例输出和数据集将是:

Soda product code = 5870
Hot Dog product code = 5730
Chips product code = 5990
Sausage Pizza product code = 5780
Pepperoni Pizza product code = 5600
Cheese Pizza product code = 5500
Vegetable Pizza product code = 5400
Mushroom Pizza product code = 5300
Meat Pizza product code = 5200
Four Cheese Pizza product code = 5100

    Description             Weight | Unit Cost  | Value
        Total Weight: 46.30         
    Soda                      46.30| $0.00      |  0

        Total Weight: 200.00            
    Hot Dogs                 200.00| $0.75      | 0.059588

        Total Weight: 100.00            
    Chips                    100.00| $0.75      | 0.315617

        Total Weight: 474.00            
    Sausage Pizza             42.00| $3.85      |0.341139241
    Peppeorni Pizza           55.00| $3.75      |0.435126582
    Cheese Pizza              72.00| $3.75      |0.569620253
    Vegetable Pizza           74.00| $3.05      |0.476160338
    Mushroom Pizza            89.00| $3.65      |0.685337553
    Meat Pizza                90.00| $3.50      |0.664556962
    Four Cheese Pizza         52.00| $3.50      |0.383966245

    Total Value of Pizzas: 3.555907173

如果这有意义,请告诉我。 要查找您所做的VALUE(产品重量/总产品重量)*单位成本。 总价值只是所有产品价值的总和。但我只需要比萨饼价值的总和。

我更喜欢使用CTE以外的东西,因为很难与我使用的系统合并。

我使用的是Microsoft SQL Server 2005。

完整代码:

SET NOCOUNT ON; 
DECLARE @Credits Decimal(19,8);
DECLARE @Price Decimal(19,8);
DECLARE @Labor Decimal(19,8);
DECLARE @Packaging Decimal(19,8);
DECLARE @Overhead Decimal(19,8);
DECLARE @ExpenseTotal Decimal(19,8);

SET @Credits = 41000;
SET @Price = 1.57;
SET @Labor = 0.1853;
SET @Packaging = 0.04;
SET @Overhead = 0.025;
SET @ExpenseTotal  = @Labor + @Packaging + @OverHead;

SELECT 
     CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN 'Trimmings' 
WHEN [PC].[ProductCategoryCode] = 5730 THEN 'Rib Tips'
WHEN [PC].[ProductCategoryCode] = 5990 THEN 'Skins'
ELSE [PC].[Description1] 
END AS [Description]
   , SUM(SUM(PL.OriginalQuantity_Stk)) OVER
 (PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
 THEN PC.ProductCategoryCode
 END)
) AS [Total Weight]
   , Sum(CASE WHEN [PC].[ProductCategoryCode] <> 5870 THEN [PL].[OriginalQuantity_Stk] 
WHEN [PC].[ProductCategoryCode] <> 5730 THEN [PL].[OriginalQuantity_Stk] 
WHEN [PC].[ProductCategoryCode] <> 5990 THEN [PL].[OriginalQuantity_Stk] 
END) AS [Weight]
   , @Credits - SUM(Sum([PL].[OriginalQuantity_Stk])) OVER () AS [Shrink]
   , CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode]) 
WHEN [PC].[ProductCategoryCode] = 5730 THEN SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode])
WHEN [PC].[ProductCategoryCode] = 5990 THEN SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode])
ELSE Sum([PL].[OriginalQuantity_Stk]) / (SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER())
END
 AS [Yield]
   , CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN [ICP].[UnitCost] 
WHEN [PC].[ProductCategoryCode] = 5730 THEN [ICP].[UnitCost] - 0.25 
WHEN [PC].[ProductCategoryCode] = 5990 THEN [ICP].[UnitCost] * 0 
ELSE [ICP].[UnitCost]
END AS [UC]
   , CASE 
 WHEN PC.ProductCategoryCode = 5870
 THEN ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits) 
 WHEN PC.ProductCategoryCode = 5730
 THEN (ICP.UnitCost - 0.25) * (Sum(PL.OriginalQuantity_Stk) / @Credits)
 WHEN PC.ProductCategoryCode = 5990
 THEN (ICP.UnitCost * 0) * (Sum(PL.OriginalQuantity_Stk) / @Credits) 
 ELSE ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / (SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER())) 
 END
 AS [Value]
   , ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) * @Labor) + ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) * @Packaging) + ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) * @OverHead) AS [Finish Total]
   , @ExpenseTotal AS [ExpenseTotal]
   , (@Price - (SUM(case when PC.ProductCategoryCode = 5730 THEN (ICP.UnitCost - 0.25) * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER()) - (SUM(case when PC.ProductCategoryCode = 5870 THEN ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER()) - (SUM(case when PC.ProductCategoryCode = 5870 THEN (ICP.UnitCost * 0) * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER() 
)) 
/ ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) / @Credits) + @ExpenseTotal AS [Cost Finish Weight]
   , (@Price - (SUM(case when PC.ProductCategoryCode = 5730 THEN (ICP.UnitCost - 0.25) * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER()) - (SUM(case when PC.ProductCategoryCode = 5870 THEN ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER()) - (SUM(case when PC.ProductCategoryCode = 5870 THEN (ICP.UnitCost * 0) * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER() 
)) 
/ ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) / @Credits) AS [ValueCalc]
   , SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits) END) OVER()
 AS [SpareTotVaL]
 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 [PL] 
     ON [DCT].LotKey = [PL].LotKey)
    LEFT OUTER JOIN  IC_ProductCosts [ICP] 
     ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5)
 WHERE 
    ([PL].ProductionDate >= { ts '2015-07-10 00:00:00' }   AND ([PL].ProductionDate <= { ts '2015-07-10 00:00:00' } OR [PL].ProductionDate Is Null)) 
AND ((1=1)  AND [ARC].CustomerKey IN (39) ) 
 GROUP BY 
     CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN 'Trimmings' 
WHEN [PC].[ProductCategoryCode] = 5730 THEN 'Rib Tips'
WHEN [PC].[ProductCategoryCode] = 5990 THEN 'Skins'
ELSE [PC].[Description1] 
END
   , [PC].ProductCategoryCode
   , [ARC].CustomerKey
   , CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN [ICP].[UnitCost] 
WHEN [PC].[ProductCategoryCode] = 5730 THEN [ICP].[UnitCost] - 0.25 
WHEN [PC].[ProductCategoryCode] = 5990 THEN [ICP].[UnitCost] * 0 
ELSE [ICP].[UnitCost]
END
   , [ICP].UnitCost
 ORDER BY 
     @Credits - SUM(Sum([PL].[OriginalQuantity_Stk])) OVER () 
   , ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) * @Labor) + ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) * @Packaging) + ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END)) OVER()) * @OverHead) 
   , SUM(SUM(PL.OriginalQuantity_Stk)) OVER
 (PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
 THEN PC.ProductCategoryCode
 END)
)

2 个答案:

答案 0 :(得分:0)

尝试删除您的Dubbelt SUM,因此没有SUM(SUM(...))

或在您执行的行中:SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits)将其更改为SUM([PL].[OriginalQuantity_Stk]) / @Credits

希望有所帮助

答案 1 :(得分:0)

使用等式:

 (SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) 
    THEN 0 
    else ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk)) END ) 
    OVER()) 
        / ((SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) 
    THEN 0 
    else PL.OriginalQuantity_Stk END )) OVER()) )

我能够得到理想的答案。