简体   繁体   English

select distinct count(id)vs select count(distinct id)

[英]select distinct count(id) vs select count(distinct id)

I'm trying to get distinct values from a table. 我正试图从表中获取不同的值。 When I ran select distinct count(id) from table I got over a million counts. 当我select distinct count(id) from table运行select distinct count(id) from table我得到了超过一百万的计数。 However if I ran select count(distinct id) from table I've got only around 300k counts. 但是,如果我select count(distinct id) from table运行select count(distinct id) from table我只有大约300k计数。 What was the difference of the two queries? 这两个查询的区别是什么?

Thanks 谢谢

When you do select distinct count(id) then you are basically doing: 当您select distinct count(id)您基本上在做:

select distinct cnt
from (select count(id) as cnt from t) t;

Because the inner query only returns one row, the distinct is not doing anything. 因为内部查询只返回一行,所以distinct不执行任何操作。 The query counts the number of rows in the table (well, more accurately, the number of rows where id is not null ). 查询计算表中的行数 (更准确地说, id不为null的行数)。

On the other hand, when you do: 另一方面,当你这样做时:

select count(distinct id)
from t;

Then the query counts the number of different values that id takes on in the table. 然后查询计算id在表中所采用的不同值的数量。 This would appear to be what you want. 这似乎是你想要的。

The second select is definitely what you want, because it will aggregate the id's (if you have 10 records with id=5 then they will all be counted as one record) and the select will return "how many distinct id's were in the table". 第二个选择肯定是你想要的,因为它将聚合id(如果你有10个id = 5的记录,那么它们将被计为一个记录)并且select将返回“表中有多少个不同的id” 。 However the first select will do something odd, and i'm not entirely sure what it will do. 然而,第一个选择将做一些奇怪的事情,我不完全确定它会做什么。

If id is the pk the count with distinct count(id) will match the no of rows returned with count(distinct id) . 如果id是pk,则具有distinct count(id)将匹配返回的具有count(distinct id)的行的count(distinct id)

If id is not the pk but has a unique constraint(on id alone, not in combination with any other column), the no of rows returned with count(distinct id) will be equal to the count with distinct count(id) , as in the case of pk. 如果id不是pk但具有唯一约束(仅在id上,不与任何其他列组合),则返回带有count(distinct id)的行的count(distinct id)将等于具有distinct count(id) ,如在pk的情况下。

If id is just another column, select count distinct count(id) from table will return one row with the no of records where the id column is NOT NULL where as select count count(distinct id) from table will return 'one column' with all non NULL unique ids in the table. 如果id只是另一列, select count distinct count(id) from table将返回one row ,其中id列为NOT NULL的记录,其中select count count(distinct id) from table将返回'one column'with表中的所有非NULL唯一ID。

In no case will the count or the no of rows returned exceed the total no of rows in your table. 在任何情况下,返回的计数或行数都不会超过表中行的总数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM