聚合函数+多个连接的异常行为

问题描述 投票:-1回答:1

这是我的RDB结构。

enter image description here

我尝试计算与单个位置相关的部门和员工数。

select street_address, count(distinct(d.department_id)), count(emp.employee_id) 
from locations loc 
inner join departments d 
on d.location_id = loc.location_id
inner join employees emp 
on emp.department_id =d.department_id
group by street_address

查询执行结果:

enter image description here

但是而不使用distinct来计算d.department_id会产生错误的结果。enter image description here

有人可以解释在查询执行期间会发生什么,为什么distinct解决此问题?

sql inner-join
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.