您必须意味着您想知道员工随着时间的推移在哪里工作。据我在 AdventureWorks2008 和 AdventureWorks2008R2 中看到的,Sales.SalesPerson 被分配了一个 TerritoryID,然后在 Sales.SalesTerritoryHistory 中,每个 BusinessEntityID 可以有多个 TerritoryID。对于那些数据库,使用这个:
select p.FirstName
,p.LastName
,e.LoginID -- there is no EmployeeID in this version of AdventureWorks
,st.Name as [TerritoryName]
from Sales.SalesPerson as sp
join Person.Person as p
on p.BusinessEntityID = sp.BusinessEntityID
join HumanResources.Employee as e
on e.BusinessEntityID = sp.BusinessEntityID
join Sales.SalesTerritoryHistory as sth
on sth.BusinessEntityID = sp.BusinessEntityID
join Sales.SalesTerritory as st
on sth.TerritoryID = st.TerritoryID
-- keep the where clause if you want to find only current TerritoryIDs
-- remove the where clause if you want to know all across time
where GETDATE() between sth.StartDate and isnull(sth.EndDate,'9999-1-1')
澄清 AdventureWorks DB 的版本,我可以纠正错误(但不要说 2012,因为我没有安装 SQL Server 2012 :)