一、普通计数查询
SELECT
`me_province` AS `省份`,
COUNT(DISTINCT IF(`me_grade` REGEXP '三级',`me_name`,NULL)) AS `三级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '二级',`me_name`,NULL)) AS `二级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '一级',`me_name`,NULL)) AS `一级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '未定级',`me_name`,NULL)) AS `未定级医院数量`
FROM
`be_hmap`
GROUP BY
`me_province`
二、汇总行(WITH ROLLUP汇总数据)
SELECT
`me_province` AS `省份`,
COUNT(DISTINCT IF(`me_grade` REGEXP '三级',`me_name`,NULL)) AS `三级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '二级',`me_name`,NULL)) AS `二级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '一级',`me_name`,NULL)) AS `一级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '未定级',`me_name`,NULL)) AS `未定级医院数量`
FROM
`be_hmap`
GROUP BY
`me_province`
WITH ROLLUP
;
三、汇总行(WITH ROLLUP;COALESCE函数处理为NULL显示“汇总”)
SELECT
COALESCE(`me_province`,'总计') AS `省份`,
COUNT(DISTINCT IF(`me_grade` REGEXP '三级',`me_name`,NULL)) AS `三级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '二级',`me_name`,NULL)) AS `二级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '一级',`me_name`,NULL)) AS `一级医院数量`,
COUNT(DISTINCT IF(`me_grade` REGEXP '未定级',`me_name`,NULL)) AS `未定级医院数量`
FROM
`be_hmap`
GROUP BY
`me_province`
WITH ROLLUP
;