Попробуйте следующее, используя оператор case
.
select
sum(case when gender = 'male' then 1 else 0 end) as total_male,
sum(case when gender = 'female' then 1 else 0 end) as total_female
from user
Если вы используете MySQL, используйте следующий
select
sum(gender = 'male') as total_male,
sum(gender = 'female') as total_female
from user
Если вы используете PostgreSQL, используйте filter
select
count(1) filter (where gender = 'male') as total_male,
count(1) filter (where gender = 'female') as total_female
from user
Вы можете получить окончательный результат, выполнив следующий запрос. вот демонстрация .
select
case
when total_male < total_female then total_female
else total_male
end as total_count
from
(
select
count(1) filter (where gender = 'male') as total_male,
count(1) filter (where gender = 'female') as total_female
from users
) t