I am trying to build a query that tells me how many distinct women and men there are in a given dataset. The person is identified by a number \'tel\'. It is possible for the sam
There is another solution similar to @segeddes's second solution
Select COUNT(DISTINCT tel) as gender_count,
COUNT(DISTINCT IF(gender = "male", tel, NULL)) as male_count,
COUNT(DISTINCT IF(gender = "female", tel, NULL)) as female_count
FROM example_dataset
Explanation :
IF(gender = "male", tel, NULL)
Above expression will return tel if gender is male else it will return NULL value
Then we've
DISTINCT
It will remove all the duplicates
And finally
COUNT(DISTINCT IF(gender = "male", tel, NULL))
Will count all the distinct occurrences of rows having male gender
Note : SQL COUNT function with expression only counts rows with non NULL values, for detailed explanation check - http://www.mysqltutorial.org/mysql-count/