Deepali Samtani
Deepali Samtani

Reputation: 53

Using two case statements in SQL Server

I want to create a separate column from 1st case statement (let's say A) and 2nd separate column from 2nd case statement (say B). Then use these columns to perform division. But this query gives the result in 1 single column renamed as pym.

select 
    WFMGrade, 
    case 
       when WFMGrade IN ('P/PA','PAT','A') 
          then sum(TotalFTE)
       when WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP') 
          then sum(TotalFTE)
       else 0
    end as pym
from 
    dw_UtilizationPredictionReport
group by 
    (WFMGrade)

How do I create separate columns for these two case statements?

Upvotes: 0

Views: 3761

Answers (2)

Milad.biniyaz
Milad.biniyaz

Reputation: 516

This works for me:

 UPDATE products
SET product_price = (
    CASE `product_home_id`
    WHEN 368794 THEN 3853070 
    WHEN 368769 THEN 938000 
    ELSE product_home_id END
),

    product_old_price = (
        CASE product_home_id 
        WHEN 368794 THEN 4000000
        WHEN 368769 THEN 1100000
        ELSE product_old_price END
    )
    
WHERE `product_home_id` IN (368794,368769)
AND `product_vendor_id` =105

Upvotes: 0

Wrap your query in two separate case statements as below:

select 
    WFMGrade, 
    case 
        when WFMGrade IN ('P/PA','PAT','A') then sum(TotalFTE)                
        else 0
    end as pym1,
    case                 
        when WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP') then sum(TotalFTE)
        else 0
    end as pym2
from 
    dw_UtilizationPredictionReport
group by 
    (WFMGrade)

Upvotes: 2

Related Questions