sql count(distinct)
时间: 2023-09-08 20:10:29 浏览: 39
The SQL COUNT(DISTINCT) function is used to count the number of unique/distinct values in a column of a table. It takes the following syntax:
```
SELECT COUNT(DISTINCT column_name) FROM table_name;
```
For example, if we have a table named "students" with columns "id", "name", and "age", and we want to count the number of unique ages in the table, we would use the following SQL query:
```
SELECT COUNT(DISTINCT age) FROM students;
```
This would return the number of distinct ages in the "students" table.
相关问题
sql count distinct
在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 ]
sql count distinct *的语法怎么用
在 SQL 中,使用 `COUNT(DISTINCT *)` 语法可以统计一张表中不重复的记录数量。以下是该语法的使用方法:
```sql
SELECT COUNT(DISTINCT *) FROM table_name;
```
其中,`table_name` 是你要进行统计的表的名称。此语句将返回表中不重复记录的数量。
需要注意的是,`COUNT(DISTINCT *)` 语法只能用于统计表中不重复记录的数量,而不能用于统计某个特定列的不重复值的数量。如果你想统计某个特定列的不重复值的数量,可以将列名替换 `*`。
示例:
```sql
SELECT COUNT(DISTINCT column_name) FROM table_name;
```
其中,`column_name` 是你要统计不重复值的列名。该语句将返回指定列中不重复值的数量。