2017-09-13 76 views
1

我正在查看一个新的列,它将包含一列原始数据与case语句结果的总和。TSQL用case case语句的结果总计一列

实施例:

SKU  StandardCost   AddOn  Combined 
---   ------------   -----  -------- 
001   0.578271    0.040194 0.618465 
070   0.290721    0.039425 0.330146 
223   0.446990    0   0.446990 

AddOn列是基于一个case语句一个计算字段。我想在我的代码中创建Combined列......有可能吗?

谢谢! 罗布=)

我与我的代码,我有工作更新...

select 
    ItemKey as 'Product Number', 
    ltrim(Rtrim([ItemKey]))+'_'+ltrim(rtrim([Plant]))+'_'+ltrim(rtrim([Location])) as 'Key', 
    [Item Desc] as 'Product Description', 
    Plant as 'Location', 
    [Location] as 'Warehouse', 
    StandardCost as 'Variable Cost', 
    --Add on costing 
    CASE 
    WHEN subString(ItemKey,1,4) = '3121' AND subString(ItemKey,10,3) = '010' then ((SELECT -- Calculating Tanker pricing for WE *NP product (Pas & Raw) 
                StandardCost 
               FROM 
                Standard_Cost 
               WHERE 
                Year=2099 --Standard Cost Variable filter 
                and ItemKey = '3121-000-010-001' 
                and [Location] = 'DNEO')- 
                (SELECT 
                StandardCost 
               FROM 
                Standard_Cost 
               WHERE 
                Year=2099 
                and ItemKey = '2121-000-010-001' 
                and [Location] = 'DNEO')) 

    WHEN subString(ItemKey,1,4) = '3141' AND subString(ItemKey,10,3) = '010' then ((SELECT -- Calculating Tanker pricing for Yolk egg product (Pas & Raw) 
                StandardCost 
               FROM 
                Standard_Cost 
               WHERE 
                Year=2099 --Standard Cost Variable 
                and ItemKey = '3141-000-010-001' 
                and [Location] = 'DNEO')- 
                (SELECT 
                StandardCost 
               FROM 
                Standard_Cost 
               WHERE 
                Year=2099 
                and ItemKey = '2141-000-010-001' 
                and [Location] = 'DNEO')) 

    WHEN subString(ItemKey,1,4) = '3181' AND subString(ItemKey,10,3) = '010' then ((SELECT -- Calculating Tanker pricing for Albumen (White) egg product (Pas & Raw) 
                StandardCost 
               FROM 
                Standard_Cost 
               WHERE 
                Year=2099 --Standard Cost Variable 
                and ItemKey = '3181-000-010-001' 
                and [Location] = 'DNEO')- 
                (SELECT 
                StandardCost 
               FROM 
                Standard_Cost 
               WHERE 
                Year=2099 
                and ItemKey = '2181-000-010-001' 
                and [Location] = 'DNEO')) 
    ELSE 0 
    END as 'Add On' 
FROM 
    Standard_Cost 

WHERE 
    [YEAR] = 2099 --2099 defines variable cost, 2017 or current year is standard cost 
    AND substring(ItemKey,4,1) <> '6' --OES products are not to show in the list 
    AND [Location] in ('AREM', 'AWAME', 'AWCHE', 'AWLEM', 'FABB') -- selected locations to display 
ORDER BY 
    1 ASC, 
    2 ASC 
+0

请发表您用来生成上面的输出的代码。 – smj

回答

1

当然... ...但你并不需要一个case语句。

select 
    * 
    ,Combined = StandardCost + AddOn 
from 
    YourTable 

如果你真的需要一个case语句,它会像...

select 
    * 
    ,Combined = case when someColumn = 'someThing' then StandardCost + AddOn end 
from 
    YourTable 
+1

谢谢,花了我几分钟的时间来理解你的简单布局如何在案例陈述中实现它。爱它!!我所做的是每当我添加> + [StandardCost] – SidCharming

+0

没有问题@SidCharming – scsimon