如何 select 有条件的行? sql,select句

How to select rows with condition? sql, select sentence

我有 table 这样的:

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

并且使用 select 句子我希望我的结果只像那些分数为 9 或以上的名字。所以基本上这意味着,例如,Henry 不能被 selected,因为他在一行中的得分低于 9,但在另一行中他的得分为 3(也应该发出空值). 我的新 table 应该是这样的:

NAME 
KAL 
ALI

我正在使用 sas 程序。谢谢!

Select NAME from YOUR_TABLE_NAME name where SCORE > 9 and score is not null

如果缺少分数,COUNT 个名字将 <> COUNT 个分数。在 having 子句中请求相等将确保您的结果集中没有分数缺失的人。

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

这里是解决方案

select name 
from table name where score >= 9 
and score <> NULL;

您可以进行聚合:

select name
from table t
group by name
having sum(case when (score < 9  or score is null) then 1 else 0 end) = 0;

如果你想要完整的行,那么你可以使用 not exists :

select t.*
from table t
where not exists (select 1 
                  from table t1 
                  where t1.name = t.name and (t1.score < 9 or t1.score is null)
                 );

您的 NULL 分数似乎被视为小于 9 的值。您也可以只使用 coalesce()min():

  select name
  from have
  group by name
  having min(coalesce(score, 0)) >= 9;

请注意,select distinctgroup by 几乎没有用处——而且 SAS proc sql 可能没有很好地优化它。