是否可以使用 group by 将这两个 sql 语句合并为一个语句?

Is it possible to combine these two sql statements into one statement using group by?

select client_type, count(gender) as num_males
from clients
where gender = 'Male'
group by client_type;
select client_type, count(gender) as num_females
from clients
where gender = 'Female'
group by client_type;

以下 SQL 语句按客户类型显示男性人数,然后按客户类型显示女性人数。我想要一个 SQL 语句来显示以下列:client_type、count(gender = 'Male')、count(gender = 'Female')。可以这样做吗?

您可以数出几个 case 表达式:

SELECT   client_type, 
         COUNT(CASE gender WHEN 'Male' THEN 1 END) AS num_males,
         COUNT(CASE gender WHEN 'Female' THEN 1 END) AS num_females
FROM     clients
GROUP BY client_type;