ggranger007
Board Regular
- Joined
- Jan 21, 2007
- Messages
- 107
My goal is to rank the top 20 Requesters in the current month. However, I also want to show how they ranked over the 11 months before this.
I am using the alias function under this MS support article to initially rank the top 20 Requesters:
http://support.microsoft.com/default.aspx?scid=kb;en-us;208946&Product=acc2000
However, I have racked my brain, trying to easily pull their rank from previous months. The ranking in the support article requires sorting on one field and assigns rank based on consecutive rows.
Here is an example of what I am trying to accomplish:
SQL of my current approach. Works to rank the top 20 Requesters, but fails for everything else. "Month-1" - "Month-12" are already defined in a subquery.
Is there any easy way to do this and return the results in one query?
I am using the alias function under this MS support article to initially rank the top 20 Requesters:
http://support.microsoft.com/default.aspx?scid=kb;en-us;208946&Product=acc2000
However, I have racked my brain, trying to easily pull their rank from previous months. The ranking in the support article requires sorting on one field and assigns rank based on consecutive rows.
Here is an example of what I am trying to accomplish:
SQL of my current approach. Works to rank the top 20 Requesters, but fails for everything else. "Month-1" - "Month-12" are already defined in a subquery.
Code:
SELECT TOP 20 (SELECT COUNT(*) FROM [04b - Sum of Delivery to Receipt Counts] Where [Month-1] >= [Month SUM Alias].[Month-1];) AS Rank, [Month SUM Alias].Requester, IIf([Month-2] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-2] >= [Month Sum Alias].[Month-2];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-2] >= [Month Sum Alias].[Month-2];),Null) AS Ranking2, IIf([Month-3] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-3] > [Month Sum Alias].[Month-3];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-3] > [Month Sum Alias].[Month-3];),Null) AS Ranking3, IIf([Month-4] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-4] > [Month Sum Alias].[Month-4];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-4] > [Month Sum Alias].[Month-4];),Null) AS Ranking4, IIf([Month-5] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-5] > [Month Sum Alias].[Month-5];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-5] > [Month Sum Alias].[Month-5];),Null) AS Ranking5, IIf([Month-6] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-6] > [Month Sum Alias].[Month-6];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-6] > [Month Sum Alias].[Month-6];),Null) AS Ranking6, IIf([Month-7] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-7] > [Month Sum Alias].[Month-7];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-7] > [Month Sum Alias].[Month-7];),Null) AS Ranking7, IIf([Month-8] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-8] > [Month Sum Alias].[Month-8];))<=20,(Select Count(*) from [MONTH SUM] Where [Month-8] > [Month Sum Alias].[Month-8];),Null) AS Ranking8, IIf([Month-9] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-9] > [Month Sum Alias].[Month-9];)+1)<=20,(Select Count(*) from [MONTH SUM] Where [Month-9] > [Month Sum Alias].[Month-9];)+1,Null) AS Ranking9, IIf([Month-10] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-10] > [Month Sum Alias].[Month-10];)+1)<=20,(Select Count(*) from [MONTH SUM] Where [Month-10] > [Month Sum Alias].[Month-10];)+1,Null) AS Ranking10, IIf([Month-11] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-11] > [Month Sum Alias].[Month-11];)+1)<=20,(Select Count(*) from [MONTH SUM] Where [Month-11] > [Month Sum Alias].[Month-11];)+1,Null) AS Ranking11, IIf([Month-12] Is Not Null And ((Select Count(*) from [MONTH SUM] Where [Month-12] > [Month Sum Alias].[Month-12];)+1)<=20,(Select Count(*) from [MONTH SUM] Where [Month-12] > [Month Sum Alias].[Month-12];)+1,Null) AS Ranking12, [Month SUM Alias].[Month-1], [Month SUM Alias].[Month-2], [Month SUM Alias].[Month-3], [Month SUM Alias].[Month-4], [Month SUM Alias].[Month-5], [Month SUM Alias].[Month-6], [Month SUM Alias].[Month-7], [Month SUM Alias].[Month-8], [Month SUM Alias].[Month-9], [Month SUM Alias].[Month-10], [Month SUM Alias].[Month-11], [Month SUM Alias].[Month-12]
FROM [04b - Sum of Delivery to Receipt Counts] AS [Month SUM Alias]
GROUP BY [Month SUM Alias].Requester, [Month SUM Alias].[Month-1], [Month SUM Alias].[Month-2], [Month SUM Alias].[Month-3], [Month SUM Alias].[Month-4], [Month SUM Alias].[Month-5], [Month SUM Alias].[Month-6], [Month SUM Alias].[Month-7], [Month SUM Alias].[Month-8], [Month SUM Alias].[Month-9], [Month SUM Alias].[Month-10], [Month SUM Alias].[Month-11], [Month SUM Alias].[Month-12]
ORDER BY [Month SUM Alias].[Month-1] DESC;
Is there any easy way to do this and return the results in one query?