您已经接近了 - 但您并没有真正查看每个销售人员在过去几年中的销售额......SalesQuota
您在表格中查看的列SalesPerson
通常是一个定义为目标的数字年份 - 这不是所有销售额的实际总和。
我会尝试这样的事情:
-- determine the SUM of all sales, for all salespeople, since Jan 1, 2008
-- SUM the total of all sales (their TotalDue value) from the SalesOrderHeader table
;WITH SalesPerPerson AS
(
SELECT
sp.BusinessEntityID,
TotalSales = SUM(soh.TotalDue) -- SUM of all sales total amounts
FROM
Sales.SalesOrderHeader soh
INNER JOIN
Sales.SalesPerson sp ON soh.SalesPersonID = sp.BusinessEntityID
WHERE
soh.OrderDate >= '20080101' -- on all sales since Jan 1, 2008
GROUP BY
sp.BusinessEntityID
)
-- from that CTE, select the sales people who have had less than 100'000$ in
-- sales since Jan 1, 2008, and display those. Show LoginID and first and
-- last name (just for information purposes)
SELECT
p.FirstName, p.LAstName,
e.LoginID,
spp.*
FROM
SalesPerPerson spp
INNER JOIN
Person.Person p ON spp.BusinessEntityID = p.BusinessEntityID
INNER JOIN
HumanResources.Employee e ON p.BusinessEntityID = e.BusinessEntityID
WHERE
TotalSales < 100000.0
因此,如果您真的只需要LoginID
,则可以将 JOIN 省略到Person.Person
表中并从输出中删除p.FirstName, p.LastName
列。