SQL查找列中不同值的数量

时间:2022-12-11 13:13:10

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

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

  • SELECT DISTINCT column_name FROM table_name;
  • 从table_name中选择不同的column_name;
  • SELECT column_name FROM table_name GROUP BY column_name;
  • 根据column_name从table_name组中选择column_name;

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

但是如何从查询中获取行数呢?子查询是必需的吗?

8 个解决方案

#1


488  

You can use the DISTINCT keyword within the COUNT aggregate function:

您可以在COUNT聚集函数中使用独特的关键字:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

This will count only the distinct values for that column.

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

#2


133  

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 distinct columnName, count(columnName) as CountOf from tableName group by columnName

#3


23  

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
/

#4


9  

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,然后给它一个别名。

#5


9  

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

This will give count of distinct group of columns.

这将给出不同列的计数。

#6


8  

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;

#7


6  

select Count(distinct columnName) as columnNameCount from tableName 

#8


-6  

Count(distinct({fieldname})) is redundant

计数(不同({ fieldname }))是多余的

Simply Count({fieldname}) gives you all the distinct values in that table. It will not (as many presume) just give you the Count of the table [i.e. NOT the same as Count(*) from table]

只需Count({fieldname})就会为您提供该表中所有不同的值。它不会(像许多人认为的那样)只给你一个表格的计数。与表中的Count(*)不同

#1


488  

You can use the DISTINCT keyword within the COUNT aggregate function:

您可以在COUNT聚集函数中使用独特的关键字:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

This will count only the distinct values for that column.

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

#2


133  

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 distinct columnName, count(columnName) as CountOf from tableName group by columnName

#3


23  

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
/

#4


9  

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,然后给它一个别名。

#5


9  

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

This will give count of distinct group of columns.

这将给出不同列的计数。

#6


8  

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;

#7


6  

select Count(distinct columnName) as columnNameCount from tableName 

#8


-6  

Count(distinct({fieldname})) is redundant

计数(不同({ fieldname }))是多余的

Simply Count({fieldname}) gives you all the distinct values in that table. It will not (as many presume) just give you the Count of the table [i.e. NOT the same as Count(*) from table]

只需Count({fieldname})就会为您提供该表中所有不同的值。它不会(像许多人认为的那样)只给你一个表格的计数。与表中的Count(*)不同