Check another duplicate answer too:
count(case when gender is null or gender not in ('Male', '1', 'Female', '2') then 1 end) as m2
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello List,
I submitted the code below:
select
(select count(*) from gemd.Respondents_tidy) - SUM (CASE WHEN gender in ('Male', '1', 'Female','2') THEN 1 ELSE 0 END) as N_missing,
SUM (CASE WHEN gender not in ('Male', '1', 'Female','2') THEN 1 ELSE 0 END) as m2
from gemd.Respondents_tidy
The code works without any error. I assume the columns of N_missing and m2 have same number. But they are not. N_missing return the correct number, but m2 give me 1 only. where is the problem of my coding? Does it means "not in" doesn't work?
I prefer the m2 way to calculate the result since it can reduce coding and easier to read.
Thanks,
Check another duplicate answer too:
count(case when gender is null or gender not in ('Male', '1', 'Female', '2') then 1 end) as m2
Try this:
SELECT COUNT(*) - SUM(CASE WHEN gender IN ('Male', '1', 'Female', '2') THEN 1 ELSE 0 END) AS N_missing,
SUM(CASE WHEN gender NOT IN ('Male', '1', 'Female', '2') THEN 1 ELSE 0 END) AS m2
FROM gemd.Respondents_tidy;
Can gender be NULL? Keep in mind that all comparisons with NULL yields UNKNOWN, because NULL is an unknown value. So if you say:
WHEN gender not in ('Male', '1', 'Female','2')
and gender is NULL, we don't know if it different from Male, since it by chance that NULL could actually be Male. That's the theory. In practice, it may be different.
For this reason, you always need to keep NULL in mind. For this particular case, the easiest way out is:
WHEN isnull(gender, '') not in ('Male', '1', 'Female','2')
isnull means that you will get back the emtpy string when gender is NULL.