count(case when )的用法

Posted on 2016-11-09 15:37  岁月向好  阅读(22058)  评论(0编辑  收藏  举报

今天接触到了 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

Copyright © 2024 岁月向好
Powered by .NET 9.0 on Kubernetes