![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA, BB...BJ, CA, CB, CC...CJ, etc in fld2. I am counting how many subcategories are listed for each category. Like A may contain 5 of AA, 7 of AB, 3 of AC, 11 of AD...1 for the rest and 20 of AJ. B may contain 2 of BA, 11 of BB, 7 of BC, and 1 for the rest. I want to pick up the top 3 subcategory counts for each category. Would look like this: Cat SubCat Count A AJ 20 A AD 11 A AB 7 B BB 11 B BC 7 B BA 2 So event though each category contains 10 subcategories, I only want to list the top 3 categories with the highest counts as above. If I just do a group by and sort I can get this: |
#3
| |||
| |||
|
|
Hello, So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA, BB...BJ, CA, CB, CC...CJ, etc in fld2. I am counting how many subcategories are listed for each category. Like A may contain 5 of AA, 7 of AB, 3 of AC, 11 of AD...1 for the rest and 20 of AJ. B may contain 2 of BA, 11 of BB, 7 of BC, and 1 for the rest. I want to pick up the top 3 subcategory counts for each category. Would look like this: Cat SubCat Count A AJ 20 A AD 11 A AB 7 B BB 11 B BC 7 B BA 2 So event though each category contains 10 subcategories, I only want to list the top 3 categories with the highest counts as above. If I just do a group by and sort I can get this: Cat SubCat Count A ... ... A A A A A A .. B ... ... B B B B B .. But I just want the top 3 of each category. The only way I can think of to do this is to query each category individually and Select Top 3, and then Union these guys into one query. The problem is that I have to hardcode each category in the Union query. There may be new categoris that I miss. Is there a way to achieve what I want without using Union? Thanks, Rich *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it! |
#4
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |