0

AdventureWorks2012:识别自 2008 年初以来销售额低于 100,000 美元的销售人员。显示:销售人员登录 ID。

我猜桌子会是

  • HumanResources.Employee
  • Sales.SalesPerson

有人可以帮助我吗?我已经花了足够的时间

SELECT 
    E.LoginID 
FROM 
    HumanResources.Employee AS E 
JOIN 
    Sales.SalesPerson AS SP ON E.BusinessEntityID = SP.BusinessEntityID 
WHERE 
    E.BusinessEntityID IN (SELECT BusinessEntityID 
                           FROM Sales.SalesPerson 
                           WHERE SalesQuota < 100000); 
4

1 回答 1

0

您已经接近了 - 但您并没有真正查看每个销售人员在过去几年中的销售额......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列。

于 2013-03-29T11:56:30.907 回答