Select Top 10 Receipts for each SalesRep

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My table contains the individual receipts of all Sales Rep viz. SalesRepID,
ReceiptID, ReceiptAmt, ReceiptDate, and each SalesRep can have multiple
ReceiptID per day.

How do I write a Select query to select top the 10 individual receipts for
each Sales Rep and then add up each Sales Rep top 10 receipts.

Thank you in advance for your help
 
SELECT Y.SalesRepID
, Sum(Y.ReceiptAmt) as TopTenSalesTotal
, Count(*) as RecordsReturned
FROM [Your Table] as Y
WHERE Y.ReceiptAmt In
(SELECT TOP 10 T.ReceiptAmt
FROM [Your Table] as T
WHERE T.SalesRepID = Y.SaleRepID
ORDER BY T.ReceiptAmt DESC)
GROUP BY Y.SalesRepID

Note that this can return more than 10 records for a SaleRepID in cases
where there are ties for the tenth position. That is why I included
Count(*) as RecordsReturned so you can see if more than ten records were
used to get the total.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top