MatthewPeters
Technical User
Hello All,
I've got a query that returns a set of customers grouped by location and then ordered by SalesYTD desc. I need the query to return only the Top 20 customers from each group/location.
I've developed a solution involving a cursor to accomplish this task. It works, but I don't like it and would prefer not to use it. I'm confident this can be done without a cursor but can't seem to figure it out. Any help would be appreciated. The query follows. Matt
Select pcNum, pcDesc,
cmNum, custName,
sum(salesMTD) as salesYTD, sum(costMTD) as salesYTD
From #CustSales
where cmNum not like 'CASH%' and cmnum not like 'ZZZZ%'
group by pcNum, pcDesc, cmNum, custName
Order By pcNum, salesYTD desc
I've got a query that returns a set of customers grouped by location and then ordered by SalesYTD desc. I need the query to return only the Top 20 customers from each group/location.
I've developed a solution involving a cursor to accomplish this task. It works, but I don't like it and would prefer not to use it. I'm confident this can be done without a cursor but can't seem to figure it out. Any help would be appreciated. The query follows. Matt
Select pcNum, pcDesc,
cmNum, custName,
sum(salesMTD) as salesYTD, sum(costMTD) as salesYTD
From #CustSales
where cmNum not like 'CASH%' and cmnum not like 'ZZZZ%'
group by pcNum, pcDesc, cmNum, custName
Order By pcNum, salesYTD desc