How can I group by on a field which has NULL values? How can I group by on a field which has NULL values? sqlite sqlite

How can I group by on a field which has NULL values?


From Aggregate Functions in SQLite

The count(X) function returns a count of the number of times that X is not NULL in a group. The count(*) function (with no arguments) returns the total number of rows in the group.

So, the COUNT function does not count NULL so use COUNT(*) instead of COUNT(y).

SELECT y, COUNT(*) AS COUNTFROM mytableGROUP BY y

Or you can also use COUNT(x) like this one.

SELECT y, COUNT(x) AS COUNTFROM mytableGROUP BY y

See this SQLFiddle


You can use isnull in SQL which gives all null values a different identity

Just Run the below query, you will get the desired results

Select y,count(isnull(y,-1)) as [Count]from mytablegroup by y