Ryan
Ryan

Reputation: 3

Query to return new columns based on the data in a single column

I saw this post (SQL - Query to seperate single column and create new columns based on column data) and wanted to comment on it, however, I don't have enough rep. I want to do the exact same thing, but I want to sum the values in the column(s) that are created for the case statements.

Upvotes: 0

Views: 47

Answers (1)

Gurwinder Singh
Gurwinder Singh

Reputation: 39527

You can apply sum on case like this(column names assumed):

select 
    name,
    sum(case when status = 'A' then value end) SUM_A,
    sum(case when status = 'B' then value end) SUM_B,
    sum(case when status = 'C' then value end) SUM_C,
    sum(case when status = 'D' then value end) SUM_D
from your_table
group by name;

Upvotes: 2

Related Questions