SQL 查找列中不同值的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/141562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:39:38  来源:igfitidea点击:

SQL to find the number of distinct values in a column

sqldistinct

提问by Christian Oudard

I can select all the distinct values in a column in the following ways:

我可以通过以下方式选择列中的所有不同值:

  • SELECT DISTINCT column_name FROM table_name;
  • SELECT column_name FROM table_name GROUP BY column_name;
  • SELECT DISTINCT column_name FROM table_name;
  • SELECT column_name FROM table_name GROUP BY column_name;

But how do I get the row count from that query? Is a subquery required?

但是如何从该查询中获取行数?是否需要子查询?

回答by Noah Goodrich

You can use the DISTINCTkeyword within the COUNTaggregate function:

您可以DISTINCTCOUNT聚合函数中使用关键字:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

This will count only the distinct values for that column.

这将只计算该列的不同值。

回答by Paul James

This will give you BOTH the distinct column values and the count of each value. I usually find that I want to know both pieces of information.

这将为您提供不同的列值和每个值的计数。我通常发现我想知道这两条信息。

SELECT [columnName], count([columnName]) AS CountOf
FROM [tableName]
GROUP BY [columnName]

回答by David Aldridge

Be aware that Count() ignores null values, so if you need to allow for null as its own distinct value you can do something tricky like:

请注意 Count() 会忽略 null 值,因此如果您需要允许 null 作为其自己的独特值,您可以做一些棘手的事情,例如:

select count(distinct my_col)
       + count(distinct Case when my_col is null then 1 else null end)
from my_table
/

回答by xchiltonx

An sql sum of column_name's unique values and sorted by the frequency:

column_name 的唯一值的 sql 总和并按频率排序:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name ORDER BY 2 DESC;

回答by xchiltonx

select count(*) from 
(
SELECT distinct column1,column2,column3,column4 FROM abcd
) T

This will give count of distinct group of columns.

这将给出不同列组的计数。

回答by Pete Karl II

SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;

you've got to count that distinct col, then give it an alias.

你必须计算那个不同的 col,然后给它一个别名。

回答by Wayne

select Count(distinct columnName) as columnNameCount from tableName 

回答by Nilesh Shinde

**

**

Using following SQL we can get the distinct column value countin Oracle 11g.

使用以下 SQL,我们可以获得Oracle 11g 中的不同列值计数

**

**

Select count(distinct(Column_Name)) from TableName

回答by Nitika Chopra

select count(distinct(column_name)) AS columndatacount from table_name where somecondition=true

You can use this query, to count different/distinct data. Thanks

您可以使用此查询来计算不同/不同的数据。谢谢

回答by Alper

After MS SQL Server 2012, you can use window function too.

在 MS SQL Server 2012 之后,您也可以使用窗口函数。

   SELECT column_name, 
   COUNT(column_name) OVER (Partition by column_name) 
   FROM table_name group by column_name ;