Search code examples
sqlselectsassentence

How to select rows with condition? sql, select sentence


I have table like this:

NAME      IDENTIFICATIONR   SCORE
JOHN         DB              10
JOHN         IT             NULL
KAL          DB              9
HENRY        KK              3
KAL          DB              10
HENRY        IP              9
ALI          IG              10
ALI          PA              9

And with select sentence I want that my result would be like only those names whose scores are 9 or above. So basically it means, that, for exaple, Henry cannot be selected, because he has score under the value of 9 in one line , but in the other he has the score of 3 (null values also should be emitted). My newtable should look like this:

NAME 
KAL 
ALI

I'm using a sas program. THANK YOU!!


Solution

  • The COUNT of names will be <> COUNT of scores if there is a missing score. Requesting equality in the having clause will ensure no person with a missing score is in your result set.

    proc sql;
      create table want as
      select distinct name from have
      group by name
      having count(name) = count(score) and min(score) >= 9;