M
MFH said:Thank you for your help. I will remember to put the details in next
time.
I just want to take the top 5 values for each record in my query.
For example: if you have a classrom of 30 students and each has taken
20 tests throughout the year. How do you display the top 5 test
scores for each student?
I tried modifying your code below and I have a data mismatch error. I
have a feeling this is not the right code for what I want to do...
Modified code...
SELECT [RG03a CORPS PN Failure Totals b].[Part Number], [RG03a CORPS
PN Failure Totals b].[Total]
FROM [RG03a CORPS PN Failure Totals b]
WHERE [RG03a CORPS PN Failure Totals b].[Total]
IN
(
SELECT TOP 5 [RG03a CORPS PN Failure Totals c].[Total]
FROM [RG03a CORPS PN Failure Totals b]
AS [RG03a CORPS PN Failure Totals c]
WHERE [RG03a CORPS PN Failure Totals c].[Part Number] =
[RG03a CORPS PN Failure Totals b].[Part Number]
ORDER BY [Total]
)
ORDER BY [RG03a CORPS PN Failure Totals b].[Part Number];
ORDER BY [Total]
Dirk Goldgar said:MFH said:Thank you for your help. I will remember to put the details in next
time.
I just want to take the top 5 values for each record in my query.
For example: if you have a classrom of 30 students and each has taken
20 tests throughout the year. How do you display the top 5 test
scores for each student?
I tried modifying your code below and I have a data mismatch error. I
have a feeling this is not the right code for what I want to do...
Modified code...
SELECT [RG03a CORPS PN Failure Totals b].[Part Number], [RG03a CORPS
PN Failure Totals b].[Total]
FROM [RG03a CORPS PN Failure Totals b]
WHERE [RG03a CORPS PN Failure Totals b].[Total]
IN
(
SELECT TOP 5 [RG03a CORPS PN Failure Totals c].[Total]
FROM [RG03a CORPS PN Failure Totals b]
AS [RG03a CORPS PN Failure Totals c]
WHERE [RG03a CORPS PN Failure Totals c].[Part Number] =
[RG03a CORPS PN Failure Totals b].[Part Number]
ORDER BY [Total]
)
ORDER BY [RG03a CORPS PN Failure Totals b].[Part Number];
Aside from the fact that this line:
ORDER BY [Total]
should have the DESC keyword applied, like this:
ORDER BY [Total] DESC
if you want the five records with the maximum [Total], I don't
immediately see anything wrong with that query. And I don't see
anything there that could give a "data mismatch error". Do you get that
error when you paste the SQL into a query window and run it? Or could
it be that you are using VB code to run this query, and maybe have an
error in that code?
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
MFH said:Actually, I want to do this with a query and not from a table. I do
not have any ID associated with this query. The query is based on
other queries the top five values I want to display for each category
are based upon totals from previous queries. Do you have any other
suggestions?
Dirk Goldgar said:Whether you select from a table or a query is irrelevant, except for the
impact it has on the efficiency of the query. So if, as I guessed
before, "RG03a CORPS PN Failure Totals b" is the name of a query and
not a table, it should have no effect on the structure of the query
you're trying to build. As I said, the query you posted doesn't look to
me as though it should raise an error, so I want to try to figure out
what caused the error you report. That's why I asked the questions I
did, but you haven't answered them: Do you get the error when you paste
the SQL into a query window and run it? Or are you using VB code to run
this query, in which case it would be a good idea to post the code.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
MFH said:It is an SQL code
MFH said:Yes, that is correct. The error message reads "Data type mismatch in
criteria expression"
MFH said:Thank you Dirk. But I have found the solution. I changed the format
in the column properties to a number format. I guess Access did not
like that there was not a format specified for this column.
MFH said:It was a standard number format with 0 decimal places. There was no
previous format. It was not a calculated field but it was based on
previous calculations in previous queries. I just changed the format
in the top 5 query and left everything else alone.