user14263992
user14263992

Reputation: 115

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

Upvotes: 1

Views: 56

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

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;

Upvotes: 1

Related Questions