question about "not in"

Kai Yang 166 Reputation points
2021-08-16T20:25:43.583+00:00

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,

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 126.1K Reputation points
    2021-08-16T21:30:30.273+00:00

    Check another duplicate answer too:

    count(case when gender is null or gender not in ('Male', '1', 'Female', '2') then 1 end) as m2

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Guoxiong 8,216 Reputation points
    2021-08-16T21:04:51.397+00:00

    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;
    

  2. Erland Sommarskog 131.4K Reputation points MVP Volunteer Moderator
    2021-08-16T21:26:42.83+00:00

    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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.