Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Find employee with most customers for the year

Author  Topic 

jon2244
Starting Member

2 Posts

Posted - 2011-06-26 : 14:09:11
I have one table that shows information for orders, including the employee ids, date, and customer associated with each order. I can find the most customers associated with a single employee with the code below, but how would I also return the corresponding employee id?

Use TSQLFundamentals2008;

SELECT orderyear,MAX(numcusts) as maxcustomers
FROM (SELECT empid, year(orderdate) AS orderyear,
COUNT(DISTINCT custid) AS numcusts
FROM Sales.Orders
GROUP BY empid, YEAR(orderdate)) Scores
GROUP BY orderyear

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-06-27 : 04:19:51
The best way to approach this (in my opinion), is with the DENSE_RANK command. Even this way there are a number of approaches depending on your business rules.

Take a look at this code:


SELECT
empid
,DATEPART(YEAR,orderdate) AS orderyear
,COUNT(DISTINCT custid) AS numcusts
,DENSE_RANK() OVER (PARTITION BY DATEPART(YEAR,orderdate) ORDER BY COUNT(DISTINCT custid) DESC/*, empid*/) AS Ranking
INTO #EmpIDRank
FROM Sales.Orders
GROUP BY empid, YEAR(orderdate)

SELECT * FROM #EmpIDRank WHERE Ranking = 1


The DENSE_RANK function uses PARTITION BY which is similar to GROUP BY - I've PARTITIONed BY the order year then ordered it by the DISTINCT COUNT of customers. Note that if two employees have the same figures it will select both. You can add the commented out code from the DENSE_RANK command to select the one with the highest or lowest empid (or use some other criteria) if you only want to return ONE result for each year.

The DENSE_RANK will return a 1 for the empid with the highest distinct customers for the order year. Then you can filter the data into a CTE or temp table WHERE Ranking = 1.

Have a look at the code and have a play with it. You may find, as I have done, that DENSE_RANK is a fantastic command that is incredibly useful.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club
http://www.hollowtreewebdesign.co.uk - a web design company in its infancy
Go to Top of Page

jon2244
Starting Member

2 Posts

Posted - 2011-06-29 : 18:15:40
quote:
Originally posted by theboyholty

The best way to approach this (in my opinion), is with the DENSE_RANK command. Even this way there are a number of approaches depending on your business rules.

Take a look at this code:


SELECT
empid
,DATEPART(YEAR,orderdate) AS orderyear
,COUNT(DISTINCT custid) AS numcusts
,DENSE_RANK() OVER (PARTITION BY DATEPART(YEAR,orderdate) ORDER BY COUNT(DISTINCT custid) DESC/*, empid*/) AS Ranking
INTO #EmpIDRank
FROM Sales.Orders
GROUP BY empid, YEAR(orderdate)

SELECT * FROM #EmpIDRank WHERE Ranking = 1


The DENSE_RANK function uses PARTITION BY which is similar to GROUP BY - I've PARTITIONed BY the order year then ordered it by the DISTINCT COUNT of customers. Note that if two employees have the same figures it will select both. You can add the commented out code from the DENSE_RANK command to select the one with the highest or lowest empid (or use some other criteria) if you only want to return ONE result for each year.

The DENSE_RANK will return a 1 for the empid with the highest distinct customers for the order year. Then you can filter the data into a CTE or temp table WHERE Ranking = 1.

Have a look at the code and have a play with it. You may find, as I have done, that DENSE_RANK is a fantastic command that is incredibly useful.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club
http://www.hollowtreewebdesign.co.uk - a web design company in its infancy


Sweet. Nice solution. Thanks a lot.
Go to Top of Page
   

- Advertisement -