My mistakes

Archive for April 2014

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.