查询返回笛卡尔积(Northwind)

时间:2016-04-06 13:15:39

标签: sql database oracle northwind

enter image description here

我在oracle中使用 northwind 数据库。

任务: 取不。在每个地区工作的员工。

结果: (RegionName,员工人数)

我正在尝试此查询,但它返回了笛卡儿产品

select r.regiondescription, count(e.employeeid)
from employees e,
     employeeterritories et,
     territories t,
     region r 
where r.regionid = t.regionid
  and et.territoryid = t.territoryid
  and e.employeeid = et.employeeid
group by r.regiondescription;

问题:我的查询有什么问题?

1 个答案:

答案 0 :(得分:3)

我唯一能想到的是,你的一张桌子正在增加结果,你应该使用count(DISTINCT)代替:

select r.regiondescription, count(distinct e.employeeid)
from employees e,
     employeeterritories et,
     territories t,
     region r 
where r.regionid = t.regionid
  and et.territoryid = t.territoryid
  and e.employeeid = et.employeeid
group by r.regiondescription;