sql count distinct
时间: 2023-08-30 07:09:42 浏览: 127
在SQL中,使用COUNT(DISTINCT 列名)可以统计某个列中不重复的唯一值的数量。例如,对于表格fromtable,可以使用以下查询语句统计userType为1和2的不重复userid的数量:
```
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 fromtable
```
这个查询语句会返回两个列,分别是类型1和类型2对应的不重复userid的数量。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [sql count()加distinct和条件去重统计](https://blog.csdn.net/u011974797/article/details/112554220)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [count()和distinct关键字的使用](https://blog.csdn.net/qq_39826207/article/details/108537874)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量](https://blog.csdn.net/weixin_55674264/article/details/125471753)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文