我坚持的查询应该列出员工销售的客户数量,并且只计算一次客户,即使他们有多个销售。我对此非常陌生,似乎无法弄明白。任何帮助是极大的赞赏!
select count(distinct o.customerid) 'Number of Customers',
e.EmployeeID 'Employee ID', e.LastName, e.firstname
from employees e
full join Orders o
on e.EmployeeID = o.employeeid
group by o.CustomerID, e.EmployeeID, e.LastName, e.firstname
order by e.employeeid, o.customerid asc
以下是我获得的结果,它表明了列表如何提供结果列表。我试图在一个字段中获取总数。希望这更有意义吗?
答案 0 :(得分:0)
你的group by子句中有customerid - 所以它会为每个customerid生成一行 - 删除它(也来自orderby),也许你会得到你需要的结果
select count(distinct o.customerid) 'Number of Customers',
e.EmployeeID 'Employee ID', e.LastName, e.firstname
from employees e
full join Orders o
on e.EmployeeID = o.employeeid
group by e.EmployeeID, e.LastName, e.firstname
order by e.employeeid