Skip to content
Advertisement

How to distribute different types of values from one column into individual columns

I have one table that stores different values in one column. Lets say the structure is like:
column_a: name
column_b: contact_type – email or phone
column_c: contact_value – exact email address or phone number
Shall a person have both email and phone, there are two rows.

I trying to get a select that would distribute the different values into respective columns:
column_a: name
column_b: email_value
column_c: phone_value
One person should have exactly one row.

So far all my attempts ended one person having two rows – email address and phone null and vice versa…

Is there a way how to distribute the two values into two respective columns and have just one record for each person?

Hope it is not too confusing question…

Advertisement

Answer

You can use conditional aggregation:

select column_a,
       max(case when column_b = 'phone' then column_c end) as phone,
       max(case when column_b = 'email' then column_c end) as email
from t
group by column_a;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement