Reputation: 3
As you see for cid=1 all date fields are NULL and for cid=3 all date fields are NOT NULL.
I need get unique cids with new field, if all dates are NULL then with "NULL" and if all dates are NOT NULL then with "NOT NULL".
cid - new field
1 - NULL
3 - NOT NULL
Upvotes: 0
Views: 50
Reputation: 1269503
You can do this with aggregation and case
:
select cid,
(case when count(datecol) = 0 then 'NULL'
when count(datecol) = count(*) then 'NOT NULL'
end) as newField
from t
group by cid
having count(datecol) in (0, count(*));
Upvotes: 2
Reputation: 42753
select cid, case when min(c) = 0 AND max(c) = 0 then 'null' when min(c) = 1 and max(c) = 1 then 'not null' end from (
select cid, case when dt is null then 0 else 1 end as c from your_table
) t
group by cid
having min(c) = max(c)
Upvotes: 2