2017-09-13 3 views
1

원시 데이터 열과 사례 문의 결과의 합계를 포함하는 새 열을 만드는 방법을 모색 중입니다.TSQL은 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

감사합니다. 케이스 레이아웃 내에서 간단한 레이아웃을 이해하는 데 몇 분이 걸렸습니다. 그것을 사랑해! 내가 한 것은 +> [표준 비용] – SidCharming

+0

아무 문제없이 @SidCharming을 추가했을 때의 각 문장에 대한 것입니다. – scsimon