Lucas Shen
Lucas Shen

Reputation: 337

sql newbie: multi variable grouping and aggregation

the raw data looks like this enter image description here

I want to transform this so I could know for each country_code, how many people register with facebook, and how many register with email.

like:

country_code|facebook|email
---------------------------
AE          |  4     | 5
AL          |  11    | 10

thanks!

Upvotes: 0

Views: 18

Answers (1)

sgeddes
sgeddes

Reputation: 62841

You can use sum with case and group by:

select country_code,
    sum(case when register_with = 'facebook' then 1 else 0 end) facebook,
    sum(case when register_with = 'email' then 1 else 0 end) email
from yourtable 
group by country_code

Upvotes: 2

Related Questions