这是我的RDB结构。
我尝试计算与单个位置相关的部门和员工数。
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
查询执行结果:
但是而不使用distinct来计算d.department_id会产生错误的结果。
有人可以解释在查询执行期间会发生什么,为什么distinct解决此问题?