I'm trying to find the top 5 sales people based on their total sales. I want to show the rep's names, and their total sales, and I'm working in AdventureWorks.
The thing that keeps throwing me off is the GROUP BY
statement. In the query below, I'm not sure why the GROUP BY
isn't working. I'm sure there's a more efficient way to do this, but at this point I'm just trying to really learn joins and subqueries, and the way in which GROUP BY
works seems to be something I'm not grasping.
SELECT SalesLT.Customer.SalesPerson, SUM(repjoin.TotalDue) OVER (PARTITION BY repjoin.salesperson)
FROM SalesLT.customer INNER JOIN
(SELECT SalesLT.SalesOrderHeader.CustomerID,
SalesLT.SalesOrderHeader.TotalDue,
SalesLT.Customer.SalesPerson
FROM SalesLT.Customer
INNER JOIN SalesLT.SalesOrderHeader
ON SalesLT.SalesOrderHeader.CustomerID = SalesLT.Customer.CustomerID
) repjoin
ON SalesLT.Customer.CustomerID = repjoin.CustomerID
GROUP BY SalesLT.Customer.SalesPerson