I would like to thank both Tom and Brian for their help - your suggestions worked perfectly. One final issue: Any reason why this query would run so slowly? I'm using Access 2002 on a P4 computer, and the db is on the C: drive. The query takes 5 minutes to run. The source table for the query only has 2003 records in it.
Here's the code - any ideas to improve performance or will I just have to live with it?
SELECT EmplID, CDATE, CBASIC_PA
FROM [qsel #4] AS T
WHERE ((([CDATE]) In (SELECT TOP 3 CDATE FROM [qsel #4] T1 WHERE [T1].[EmplID] = [T].[EmplID] ORDER BY CDATE DESC)));
----- Tom Ellison wrote: -----
It's certainly true that the date value needs to be in date/time
format (a numeric, not a text datatype) in order for the ORDER BY
clause to put the rows of the subquery in actual reverse date order,
which could likely be different from "alphabetical" order. As Brian
says, if the DateNum column is text, you may be able to convert it to
a date value. I'm thinking this would be just a change like this:
ORDER BY CDATE(DateNum) DESC
Notice that CDATE() is a function, which is a pretty good reason to
NOT name a column with that same name. However, I believe the syntax
may be unambiguous, so I'm thinking this won't cause a problem for
now.
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
To add to Tom's post, if CDATE is a Date/Time field, you don't need to
convert it to a number. If it's a Text field, you should probably be using
a Date field instead, or at least using CDate to convert it to a Date/Time
value (in case that's not what you're already doing to get DateNum).
If CDATE is a Date/Time field, you could remove DateNum from the outer
SELECT clause, and replace it with CDATE elsewhere.
I'm trying to create a query that shows me the top 3 dates by employee ID.
I've converted the date field (CDATE) to a number value (DateNum), and I'm
using the following SQL:
SELECT [qsel #4].RecID, [qsel #4].EmplID, [qsel #4].CDATE, [qsel
#4].CBASIC_PA, [qsel #4].DateNum
FROM [qsel #4], [qsel #4] AS [qsel #4_1]
WHERE ((([qsel #4].DateNum) In (SELECT TOP 3 [qsel #4].DateNum FROM [qsel
#4] WHERE [qsel #4].[EmplID] = [qsel #4_1].[EmplID] ORDER BY [qsel
#4].DateNum DESC)));
Access stops responding. How should I modify the query to get the Top 3 DateNum within each EmplID?