SQL使用CASE计算值

时间:2015-07-22 19:04:52

标签: sql sql-server-2005 case running-total

我使用CASE根据产品类别计算商品的总重量。

为此我正在使用代码:

CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode]) 
WHEN [PC].[ProductCategoryCode] = 5730 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode])
WHEN [PC].[ProductCategoryCode] = 5990 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode])
ELSE SUM(SUM([PL].[OriginalQuantity_Stk])) OVER()
END
 AS [Total Weight]

5870,5730,5990都显示正确的总重量。问题出在ELSE部分。其余的值都有不同的产品类别代码,但它们需要组合在一起,我需要这些代码的总重量。目前,ELSE部分占据了所有项目的总和,包括产品5870,5730,5990。

例如:

Product Category Code | Weight |
5870                  | 100    |
5730                  | 400    |
5990                  | 200    |
1111                  | 50     |  
2222                  | 175    |  
3333                  | 500    |  
4444                  | 125    |  

5870 = 100
5730 = 400
5990 = 200
ELSE = 850

整个代码:

SET NOCOUNT ON; 
DECLARE @Credits Decimal(19,8);
DECLARE @Price Decimal(19,8);

SET @Credits = 41000;
SET @Price = 1.57;

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]
   , CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode]) 
WHEN [PC].[ProductCategoryCode] = 5730 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode])
WHEN [PC].[ProductCategoryCode] = 5990 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode])
ELSE SUM(SUM([PL].[OriginalQuantity_Stk])) OVER()
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(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode])
WHEN [PC].[ProductCategoryCode] = 5730 THEN ([ICP].[UnitCost] - 0.25) * SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode])
WHEN [PC].[ProductCategoryCode] = 5990 THEN ([ICP].[UnitCost] * 0) * SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode])
ELSE [ICP].[UnitCost] * SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits) OVER (PARTITION BY [PC].[ProductCategoryCode])
END AS [Value]
   , CASE WHEN [PC].[ProductCategoryCode] = 5780 OR [PC].[ProductCategoryCode] < 5700 THEN SUM(Sum([PL].[OriginalQuantity_Stk]) / @Credits * [ICP].[UnitCost])
OVER (PARTITION BY [PC].[ProductCategoryCode])
END AS [TotalValue]
 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
   , [ARC].CustomerKey
   , [PC].ProductCategoryCode
   , 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 () 
   , CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode]) 
WHEN [PC].[ProductCategoryCode] = 5730 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode])
WHEN [PC].[ProductCategoryCode] = 5990 THEN SUM(SUM([PL].[OriginalQuantity_Stk])) OVER (PARTITION BY [PC].[ProductCategoryCode])
ELSE SUM(SUM([PL].[OriginalQuantity_Stk])) OVER()
END

我正在使用Microsoft SQL Sever 2005。

2 个答案:

答案 0 :(得分:1)

如果我理解正确,你想把case 放在窗口函数中:

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

答案 1 :(得分:0)

您可以使用以下两个查询来执行此操作:

SELECT P.[PseudoCode], SUM(P.[OriginalQuantity_Stk]) AS CalcWeight FROM (
    SELECT CASE
        WHEN [PC].[ProductCategoryCode] = 5730 THEN '5730'
        WHEN [PC].[ProductCategoryCode] = 5870 THEN '5870'
        WHEN [PC].[ProductCategoryCode] = 5990 THEN '5990'
            ELSE 'ELSE' END AS PseudoCode, [PL].[OriginalQuantity_Stk]
    FROM IC_Products [PC]) P
GROUP BY PseudoCode

另外,在你的查询中你有太多的case语句......你是否考虑设置一个表变量并将每个ProductCategoryCode的所有不同值放在其中然后只是加入这个表?代码将更容易阅读和维护。