我有这个查询,这让我很难弄清楚问题是什么,感谢您的建议。
SELECT
cooperatives.island, cooperatives.id,
COUNT(
CASE WHEN members.gender = 'Male' THEN 1 ELSE NULL END
) AS males,
COUNT(
CASE WHEN members.gender = 'Female' THEN 1 ELSE NULL END
) AS females,
COUNT(members.gender) AS genderall
FROM
cooperatives
JOIN members ON cooperatives.id= members.c_id
WHERE
1 = 1
GROUP BY
cooperatives.island
我想做的是获得这样的结果
哪个有效但是我需要添加的内容还有另一个专栏,它将显示每个岛屿的一些合作社。
答案 0 :(得分:2)
我怀疑你想要:
SELECT c.island, COUNT(DISTINCT c.id) as num_cooperatives,
SUM(m.gender = 'Male') AS males,
SUM(m.gender = 'Female') AS females,
COUNT(m.gender) AS genderall
FROM cooperatives c JOIN
members m
ON c.id = m.c_id
GROUP BY c.island;