对于我的报告,我有几个不同的产品代码。这些产品代码由两部分组成。 5870,5730和5990是一个部分(上半部分)和5780,小于5700的部分构成另一个部分(下半部分)。我已经有一个代码,它结合了5780和少于5700的所有内容,即:
SUM(SUM(PL.OriginalQuantity_Stk)) OVER
(PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
THEN PC.ProductCategoryCode
END)
)
此输出的示例是:
Product Category Code | Weight |
5870 | 100 |
5730 | 400 |
5990 | 200 |
5780 | 50 |
1111 | 50 |
2222 | 175 |
3333 | 500 |
4444 | 125 |
5870 = 100
5730 = 400
5990 = 200
Other= 900
该代码正是我需要它为报告的这一部分做的。但是,该代码还会生成其他产品代码的SUM,这对于报告的这一部分非常有用,但对于另一个计算,我只需要使用产品代码5780以及少于5700的所有产品代码。
现在我正在尝试使用代码:
(CASE WHEN PC.ProductCategoryCode = 5780 OR PC.ProductCategoryCode < 5700 THEN Sum(Sum(PL.OriginalQuantity_Stk)) OVER(PARTITION BY PC.ProductCategoryCode)
ELSE 0
END)
这使得不相关的产品代码的权重等于零,但它不需要我需要的产品代码权重的总和。
我现在需要的一个示例输出是:
Product Category Code | Weight |
5870 | 100 |
5730 | 400 |
5990 | 200 |
5780 | 50 |
1111 | 50 |
2222 | 175 |
3333 | 500 |
4444 | 125 |
5870 = 0
5730 = 0
5990 = 0
Other= 900
它不一定必须等于0,但我只需要900值,因为我将不得不在本报告中将900用于其他计算。我希望这是有道理的。 如果我需要澄清更多,请告诉我。
我使用的是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);
SET @Credits = 41000;
SET @Price = 1.57;
SET @Labor = 0;
SET @Packaging = 0;
SET @Overhead = 0;
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]) / @Credits
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) / @Credits)
END AS [Value]
, SUM(ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits)) OVER
(PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
THEN PC.ProductCategoryCode
END)
) AS [Total Value]
, (CASE WHEN PC.ProductCategoryCode = 5780 OR PC.ProductCategoryCode < 5700 THEN Sum(Sum(PL.OriginalQuantity_Stk)) OVER(PARTITION BY PC.ProductCategoryCode)
ELSE 0
END) AS [Test]
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
, [PC].ProcessCode
ORDER BY
@Credits - SUM(Sum([PL].[OriginalQuantity_Stk])) OVER ()
, SUM(SUM(PL.OriginalQuantity_Stk)) OVER
(PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
THEN PC.ProductCategoryCode
END)
)
, SUM(ICP.UnitCost * (Sum(PL.OriginalQuantity_Stk) / @Credits)) OVER
(PARTITION BY (CASE WHEN PC.ProductCategoryCode IN (5870, 5730, 5990)
THEN PC.ProductCategoryCode
END)
)
答案 0 :(得分:0)
我能够使用
SUM(SUM(case when PC.ProductCategoryCode IN (5870, 5730, 5990) THEN 0 else PL.OriginalQuantity_Stk END )) OVER()
获得我想要的价值。