I am able to replicate my issue with a very simple case..
Explanation
I have a very simple table my_table with one column column1.
create table my_table (column1 varchar(58));
I have few values for this column, NULL is also one of them.
insert into my_table (column1) values ('value1'), ('value1'), ('value2'), (null), ('value2');
Problem
When I try to query for group by column1 It is giving expected results by grouping all NULLs together. However if I add a where clause on column1 something like
select count(1) as value_count, column1 from my_table where column1 <> 'value1' group by column1;
It is ignoring both value1 and NULL where I was expecting to ignore only value1.
With this simple case I could get a workaround for this by adding an OR condition, But it is a real pain to add this condition all over in my original case.
Could someone can explain me better why this behavior and how can I fix this?
This is because any comparison with a NULL does not produce a true or false result, but instead produces a NULL result. Consequently, the condition
column1 <> 'value1'evaluates as NULL wherecolumn1is NULL, and so NULL values are not selected.You can get around this by using a function such as
coalesceto testcolumn1– like so: