sql count()加distinct和条件去重统计 |
您所在的位置:网站首页 › 如何去重计数 › sql count()加distinct和条件去重统计 |
表数据: userid userType ------------------------------------------ A 1 B 1 B 1 C 2需求:查出userType=1和userType=2的用户数,并且直接用字段展示出来,可能还有很多其他类型,也需要同时展示。 去重加上条件查询: select COUNT(DISTINCT case when userType=1 then userid else null end) as 类型1, COUNT(DISTINCT case when userType=2 then userid else null end) as 类型2 from table解析:distinct会把userid去重统计,如果是null就不会统计,如果是按其他字段去重,可以把userid替换成其他字段名。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |