My mistakes

Posted on: April 25, 2014


MySQL distinct count if conditions unique

Build a query to find distinct women and men in a given records. 
The row is identified by the field id and it may be duplicated , but the gender for an id should only be counted once.

SELECT
    COUNT(DISTINCT CASE WHEN gender ='m' THEN id END) as male_count,  
    COUNT(DISTINCT CASE WHEN gender ='f' THEN id END) as female_count
FROM table.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: