all groups > sql server mseq > march 2005 >
You're in the

sql server mseq

group:

Top Keyword with Group By


Top Keyword with Group By Khris
3/10/2005 11:41:07 AM
sql server mseq:
Is it possible to retreive the Top n columns for each
distinct value in the Group By?
Example Table has Divisions, Customers and Invoices.
I want to return the Top 10 Customers for each Division
based on the Sum(Invoices).
NOTE, I'm trying to do this with Data Warehouse data so
Re: Top Keyword with Group By Hugo Kornelis
3/10/2005 11:24:20 PM
[quoted text, click to view]

Hi Khris,

Try if this helps:

SELECT a.Division, a.Customer, SUM(a.Invoices)
FROM YourTable AS a
WHERE a.Customer IN (SELECT TOP 10 b.Customer
FROM YourTable AS b
WHERE b.Division = a.Division
GROUP BY b.Customer
ORDER BY SUM(b.Invoices))
GROUP BY a.Division, a.Customer

Best, Hugo
--

AddThis Social Bookmark Button