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/
Here's one option using a subquery with DISTINCT
:
SELECT COUNT(*) gender_count,
SUM(IF(gender='male',1,0)) male_count,
SUM(IF(gender='female',1,0)) female_count
FROM (
SELECT DISTINCT tel, gender
FROM example_dataset
) t
This will also work if you don't want to use a subquery:
SELECT COUNT(DISTINCT tel) gender_count,
COUNT(DISTINCT CASE WHEN gender = 'male' THEN tel END) male_count,
COUNT(DISTINCT CASE WHEN gender = 'female' THEN tel END) female_count
FROM example_dataset