SQL 聚合未产生结果
为什么这会给出空结果?我正在尝试获取非主管员工的最小值、最大值和平均值。
SELECT MIN(salary), MAX(salary), AVG(salary)
FROM employee
WHERE ssn NOT IN
(SELECT superssn FROM employee)
员工表的一部分
SSN SUPERSSN
--------- ---------
888665555
333445555 888665555
987654321 888665555
987987987 987654321
123456789 333445555
999887777 987654321
666884444 333445555
453453453 333445555
Why is this giving an empty result? I'm trying to get the min, max, and average of the set of employees who aren't supervisors.
SELECT MIN(salary), MAX(salary), AVG(salary)
FROM employee
WHERE ssn NOT IN
(SELECT superssn FROM employee)
Part of employee table
SSN SUPERSSN
--------- ---------
888665555
333445555 888665555
987654321 888665555
987987987 987654321
123456789 333445555
999887777 987654321
666884444 333445555
453453453 333445555
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要从内部查询中排除空值。否则,外部查询将检查列表,其中列表值之一为 NULL,并且该比较不会返回任何内容。所以,正确的查询应该是这样的。
You need to exclude nulls from the inner query. Otherwise the outer query checks against a list where one of the list values is NULL and that comparison doesn't return anything. So, the correct query should be something like.
这就是为什么 NOT EXISTS 在语义上是正确的并且更可靠的原因
NOT IN 列表中的任何 NULL 总是会由于布尔逻辑而给出错误。您的 superssn 值为 NULL。
This is why NOT EXISTS is semantically correct and more reliable
Any NULL in the NOT IN list will always give false thanks to boolean logic. You have a NULL superssn value.