今天接触到了 count(case when ……)的用法,挺棒的。
表数据:
编号 性别
ID | gender |
7136609221 | male |
7136609222 | male |
7136609223 | female |
7136609228 | male |
7136609222 | male |
7136609223 | female |
语句:
SELECT COUNT(DISTINCT tel) gender_count,
COUNT(DISTINCT CASE WHEN gender = 'male' THEN tel END) male_count,
COUNT(DISTINCT CASE WHEN gender = 'female' THEN tel END) female_count
FROM example_dataset
http://stackoverflow.com/questions/19672001/mysql-distinct-count-if-conditions-unique