How to count null values in postgresql?
Solution 1
Use count(*)
:
select count(*) from train where "column" is NULL;
count()
with any other argument counts the non-NULL values, so there are none if "column"
is NULL
.
Solution 2
Use SUM
SELECT SUM(CASE WHEN column IS NULL THEN 1 ELSE 0 END) AS column_null_tally
FROM table;
Solution 3
Some workaround when you want to count the values on aggregations, including NULL ones, but can't use count(*)
(if other columns are different too).
On these cases, you can use this request :
count(distinct("column")) + (CASE bool_or("column" is null) WHEN true THEN 1 ELSE 0 END)
The count(distinct(column))
will count the non null values, and the other part will add 1
if there is a null value
Surjya Narayana Padhi
I feel passionate to work in computer vision. It like a magic to me. Giving machines intelligence is so cool thing. I like to work various projects which are cool. Like to learn any technology on my way to accomplish a cool project. Exploring data,applying machine learning techniques for prediction or statistical methods to get insights is so awesome. Creating nice visualization using python is so nice. My hobby : Reading recent science news, Astrology, music
Updated on June 07, 2022Comments
-
Surjya Narayana Padhi almost 2 years
select distinct "column" from table;
output:
column 1 0.0 2 [null] 3 1.0
But when I try to count the null values
select count("column") from train where "column" is NULL;
Gives output 0 (zero)
Can you suggest where it's going wrong?