Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "SELECT COUNT(DISTINCT <Column>) FROM <Table>" return 0?

Tags:

sql

database

I have run the query above for all columns in a massive table (Billion rows) and everything is fine except a couple which are returning 0. How is this possible?

like image 694
Stefan Orr Avatar asked Aug 17 '15 02:08

Stefan Orr


1 Answers

COUNT(DISTINCT) can return zero under two circumstances. The first is that all the values for the column/expression evaluate to NULL. The second is that the WHERE clause (or JOIN) filters out all rows.

If you have no WHERE or JOIN, then the values are all NULL for <Columnb>.

like image 70
Gordon Linoff Avatar answered Oct 10 '22 04:10

Gordon Linoff