I think I'm starting to get the hang of using RANK or ROW_NUMBER with partition in SQL. However, I've got one now that seems like it should be simple - Excel makes it look terribly simple with a Pivot Table - but I can't seem to match the results so far in SQL.
I'm hoping I'm just missing something simple.
I want to get the top 10 Names per month, ordered by the count of records associated with the names.
The Names have a one to many relationship... as in many records in this table can be linked to each Name.
For the months, I have a calculated field in the same query that breaks these into groups by month.
----------------
Example Data from table:
ID MyDate Name OtherFields
111 01/01/11 Leroy MiscUnimportantInfo
112 02/07/11 Leroy MiscUnimportantInfo
113 02/10/11 Leroy MiscUnimportantInfo
114 04/07/11 Leroy MiscUnimportantInfo
125 08/09/11 Leroy MiscUnimportantInfo
135 10/03/11 Leroy MiscUnimportantInfo
211 01/05/11 Jethro MiscUnimportantInfo
212 02/17/11 Jethro MiscUnimportantInfo
213 02/12/11 Jethro MiscUnimportantInfo
214 04/17/11 Jethro MiscUnimportantInfo
225 08/19/11 Jethro MiscUnimportantInfo
235 10/05/11 Jethro MiscUnimportantInfo
711 01/11/11 Billy Bob MiscUnimportantInfo
712 02/12/11 Billy Bob MiscUnimportantInfo
713 02/15/11 Billy Bob MiscUnimportantInfo
714 04/07/11 Billy Bob MiscUnimportantInfo
725 08/21/11 Billy Bob MiscUnimportantInfo
735 10/30/11 Billy Bob MiscUnimportantInfo
............
From this, I want the top 10 by count of each name per month... something like this:
MyMonth Name
Jan '11 Jethro
Jan '11 DonaldDuck
...
Feb '11 TheMouse
Feb '11 BillyBob
..
Mar '11 Jethro
Mar '11 BillyBob
..
Apr '11 SomebodySpecial
Apr '11 SomebodyNotSoSpecial
..
----------------
Sorry for the wordy explanation of my situation. Hopefully the example gives enough detail to make sense.
So this is what I've been trying:
The above query is not giving me what I need. I get duplicates on the Name field, where I really should just have one of each per month .... so the "top 10 names for each month" is giving me say 4 or 5 names, and one of the names 5 to 6 times.
Thanks for any advice, examples, references, etc.
"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
I'm hoping I'm just missing something simple.
I want to get the top 10 Names per month, ordered by the count of records associated with the names.
The Names have a one to many relationship... as in many records in this table can be linked to each Name.
For the months, I have a calculated field in the same query that breaks these into groups by month.
----------------
Example Data from table:
ID MyDate Name OtherFields
111 01/01/11 Leroy MiscUnimportantInfo
112 02/07/11 Leroy MiscUnimportantInfo
113 02/10/11 Leroy MiscUnimportantInfo
114 04/07/11 Leroy MiscUnimportantInfo
125 08/09/11 Leroy MiscUnimportantInfo
135 10/03/11 Leroy MiscUnimportantInfo
211 01/05/11 Jethro MiscUnimportantInfo
212 02/17/11 Jethro MiscUnimportantInfo
213 02/12/11 Jethro MiscUnimportantInfo
214 04/17/11 Jethro MiscUnimportantInfo
225 08/19/11 Jethro MiscUnimportantInfo
235 10/05/11 Jethro MiscUnimportantInfo
711 01/11/11 Billy Bob MiscUnimportantInfo
712 02/12/11 Billy Bob MiscUnimportantInfo
713 02/15/11 Billy Bob MiscUnimportantInfo
714 04/07/11 Billy Bob MiscUnimportantInfo
725 08/21/11 Billy Bob MiscUnimportantInfo
735 10/30/11 Billy Bob MiscUnimportantInfo
............
From this, I want the top 10 by count of each name per month... something like this:
MyMonth Name
Jan '11 Jethro
Jan '11 DonaldDuck
...
Feb '11 TheMouse
Feb '11 BillyBob
..
Mar '11 Jethro
Mar '11 BillyBob
..
Apr '11 SomebodySpecial
Apr '11 SomebodyNotSoSpecial
..
----------------
Sorry for the wordy explanation of my situation. Hopefully the example gives enough detail to make sense.
So this is what I've been trying:
Code:
SELECT x.MyMonth,x.Name
FROM (
SELECT LEFT(DATENAME(month,t.MyDate),3) + ' ''' + RIGHT(YEAR(t.MyDate),2) AS MyMonth ,t.Name
,ROW_NUMBER() OVER(PARTITION BY LEFT(DATENAME(month,t.MyDate),3) + ' ''' + RIGHT(YEAR(t.MyDate),2)
ORDER BY t.Name DESC) AS RowNum
FROM MyTable t
) x
WHERE x.RowNum < 11
The above query is not giving me what I need. I get duplicates on the Name field, where I really should just have one of each per month .... so the "top 10 names for each month" is giving me say 4 or 5 names, and one of the names 5 to 6 times.
Thanks for any advice, examples, references, etc.
"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57