首页 > 解决方案 > SQL Query count with group by 并且至少填写了一个日期

问题描述

我需要编写一个 sql 查询来计算未被推荐的员工人数。每个员工可以有多个带有成员评论的记录。如果它们被推荐,那么特征日期将具有日期,否则将具有空值。我想忽略那些至少出现过一次的人(将在功能日期列中关联一个日期)

例如:

Empid   comment     featuredate
101      cool       null
101      bad        2/2/2020
102      nice       null
102      not nice   null

所以我的查询应该只返回 empid 102,因为它们至少没有出现一次。

我试过什么?

我尝试分组和拥有,但卡在那里。

标签: sqlsql-server

解决方案


您可以使用聚合:

select empid
from t
group by empid
having max(featuredate) is null;

注意:这假设所有员工都在您的表中。如果没有,请使用not exists

select e.*
from employees e
where not exists (select 1
                  from features f
                  where f.empid = e.empid and f.featuredate is not null
                 );

如果您只想要计数,则可以将其中任何一个用作子查询。或者只是select count(*)与第二个查询一起使用。


推荐阅读