View Full Version : TOP "N" records by group


DanG
05-11-2005, 03:16 PM
I have a query that has the following...
AdvisorName Sales Class
Bill Jones $50000 Vetran
Tim Taylor $46000 Vetran
Mr Grumpy $10000 Roockie
Sid Vicous $90000 Rookie

The end result I'd like to show..
AdvisorName Sales Class
Bill Jones $50000 Vetran
Mr Grumpy $10000 Roockie
The result being the "Top" (1 or 5...) by group (Vetran or Rookie...)
I know how to do top values but not by group.

Any pointers in the right direction would be greatly appreciated.
Thank you :confused:

Mile-O
05-11-2005, 03:20 PM
I'd have a combobox on a form where you can select Veteran or Rookie and reference this combobox in the query's WHERE clause.

i.e.

SELECT TOP 5 *
FROM MyTable
WHERE Class = [Forms]![MyForm]![MyCombobox];


Also, break AdvisorName into Forename and Surname - it's best practice. :)

DanG
05-11-2005, 03:47 PM
I am doing a top X to fill in the following excel template. The left side is the Year to date and the right side is the Week to date. It is by net sales and by group (Vetran, Rookie...) . This sheet is for 1 product group, we have about eight. The query question I had given you is for YTD but in the end I will have anther query (s) to fill the WTD half. I thought it might help if you saw the big picture.
Thank you very much.

Year-to-Date Weekly

Veterans
1 bob smith $50000 1 Dan Pickle $16
2 carl wilsn $40000 2 Vickey Vixon $5
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

2001-2003 Hire Date
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

Rookie
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

Pre-Rookie
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10