Skip to content

addition within sum case when

enter image description here

How to sum all the customers in DS01,02,03 into DS? enter image description here

select sum(case when Ticket=’DS01′ OR ‘DSO2’ OR ‘DS03′ THEN customer ELSE 0 END) AS DS sum(case when Ticket=’MSO2’ OR ‘MS03’ THEN customer ELSE 0 END) AS MS from table group by ticket unsure how to bring in addition condition within “THEN and ELSE ” part

Advertisement

Answer

You can use left():

select left(ticket, 2), sum(customers)
from t
group by left(ticket, 2);

Or, if you want explicit lists:

select (case when ticket in ('DS01', 'DS02', 'DS03') then 'DS'
             when ticket in ('MS02', 'MS03') then 'MS'
        end), sum(customers)
from t
group by 1;
User contributions licensed under: CC BY-SA
6 People found this is helpful