COUNT()不适用于GROUP BY?

时间:2014-05-16 15:52:42

标签: sql sqlite count group-by

SELECT COUNT(*) FROM table GROUP BY column

我从表中获取总行数,而不是GROUP BY后的行数。为什么呢?

2 个答案:

答案 0 :(得分:6)

因为这就是group by的工作原理。它为源数据中每个标识的行返回一行。在这种情况下,它将为每个组提供计数。

获得你想要的东西:

select count(distinct column)
from table;

编辑:

稍微注意,如果column可以是NULL,则真正的等价物是:

select (count(distinct column) +
        max(case when column is null then 1 else 0 end)
       )
from table;

答案 1 :(得分:2)

试试这个:

SELECT COUNT(*), column
FROM table
GROUP BY column
相关问题