我必须显示摘要,以每个单位中的男性人数,女性人数表示。尝试了以下查询:

SELECT
    unit,
    CASE gender
        WHEN 'Male' THEN COUNT(id)
    END AS 'male',
    CASE
        WHEN gender = 'Female' THEN COUNT(id)
    END AS 'female'
FROM
    admission_info
WHERE
    admission_date = '2017-04-15'
GROUP BY gender


这是显示输出的Fiddle。但是我希望它在1行中显示单位名称,男性人数,女性人数。

最佳答案

SELECT unit,
sum(CASE gender WHEN 'Male' THEN 1 else 0 END) AS 'male',
sum(CASE WHEN gender='Female' THEN 1 else 0 END) AS 'female'
FROM admission_info WHERE admission_date='2017-04-15'
group by unit

关于mysql - MySQL GROUP BY没有给出期望的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43422280/

10-13 08:16