I have data that looks like this:
Column1 | Column2 |
---|---|
value1 | x |
value1 | x |
value1 | (null) |
value2 | y |
value2 | y |
value3 | (null) |
value3 | (null) |
I want to Count Distinct values in Column1 and Group by Column 2 where it ignore nulls unless null is the only value. expected result would be:
Column2 | Count Distinct Column1 |
---|---|
x | 1 |
y | 1 |
(null) | 1 |
any help is appreciated. thanks
2
Answers
OP here.
I was able to answer my question. I ended up using a union between 2 queries to get what I was looking for. the first query counts the non-null groups and the second counts the null group but with a CTE to exclude values already counted in the first query.
code I used below:
Thanks for the input!
I assume you mean to "ignore rows where
column2
is null, unless thecolumn1
value does not appear in other rows" – like the "value3" that appears only in rows wherecolumn2
is null?You can do such filtering like this:
(online demo)