sql - 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,因为它们至少没有出现一次。
我试过什么?
我尝试分组和拥有,但卡在那里。
解决方案
您可以使用聚合:
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(*)
与第二个查询一起使用。
推荐阅读
- python - 从熊猫系列时代获取星期几
- html - 是否可以将“下一个按钮”添加到具有页面编号列表的网站?
- python - Snakemake 声明规则以非零退出代码退出,即使使用“|| true”?
- json - oetl.sh 错误:不支持输入类型:: 类 java.lang.Integer [OETLOrientDBLoader]
- java - 单击按钮时如何连续使用编辑文本获取输入,删除编辑文本中的前一个值
- firebase - 使用 FLUTTER_NOTIFICATION_CLICK 时,Flutter 的 Firebase 云消息的数据有效负载中的 ID 和状态选项是什么?
- javascript - 我已经在 innerHTML 中计算了距离和时间,但我无法使用文本框中的 id 传递它。我该如何解决?
- powershell - 为什么 ManufacturerName 和 UserFriendlyName 不完整?
- unity3d - Spherecasting 没有击中我想要的位置
- css - 为什么我的 cpanel 或网站没有使用我刚刚添加的新 css 文件?