I probably did not state my objectives/task very well in my original post.
After further thought and reading this entire dialog I decided that a Query
is not what I needed to accomplish this task. This data is in a table used
to send e-mails to employees notifying them that they need to review a
document. It does this everyday until they complete the document. (I call
this table the "Queue".) Each document is designated with a number and a rev.
Employees are identified with a clock number. Each employee will have
different documents that s/he is required to complete. Once they complete a
document, it is removed from the queue. I have routines to do all this no
problem. The table will only have about 400 records in it at any given time
after the solution is implemented.
The wrinkle comes in that some employees don't complete the requirement
before a new revision is assigned. When this happens, they are sent messages
requiring them to complete two (or more) revisions of the same document,
which has been deemed as unnecessary. Some employees don't have e-mail
addresses, so the messages are sent to their supervisor. Supervisors in some
cases are being overwhelmed by uneeded messages relative to old revisions for
their employees. I don't want to keep placing records into a table without
ever cleaning out the ones no longer needed.
The table contains fields for the [Key], [DocumentNumber], [DocumentRev],
[ClockNumber] and [DeleteFlag] among others. The [Key] is the Primary Key, a
unique identifier consisting of the [DocumentNumber] & [Rev] &
[ClockNumber].
I created a form that opens in "Hidden" mode. The Record Source for the
form is:
SELECT ClockNumber, Key, DocumentNumber, DocumentRev, DeleteFlag FROM
TrainingDocsQueueTable ORDER BY ClockNumber, Key DESC;
This presents the records so that each users records is grouped together
(ClockNumber) with the latest rev for similar documents at the top of each
section for similar documents. (Key). The data looks sort of like this.
1111 10-01B1111 10-01 B 0
1111 10-01A1111 10-01 A 0
2345 20-10D2345 20-10 D 0
2345 20-10C2345 20-10 C 0
ETC.
I then wrote a series of three nested do loops to start at the top record
for a specific clock number. Mark the delete flag for that record as false.
Go to the next record, if the clock number is the same as the previous record
and the document number is the same, mark the delete flag as true. Continue
on through all the records , close the form and run a delete query to delete
all records where the delete flag is True.
This may not be the best way to do it, but I think it works pretty good.
Can anyone see anything wrong with it?
Ken, I am going to fool around with your latest suggesion when I have time,
it looks as if there may be something there that I could have also used.
Thanks to all for your ideas and assistance along the way.
Ken Sheridan said:
Try this:
DELETE *
FROM YourTable AS T1
WHERE MID(Key,6,1) <>
(SELECT MAX(MID(Key,6,1))
FROM YourTable AS T2
WHERE LEFT(T2.Key,5) = LEFT(T1.Key,5)
AND RIGHT(T2.Key,5) = RIGHT(T1.Key,5));
The way it works is that the subquery is correlated with the outer query on
the five left and right characters of the key, so the subquery returns the
row with the maximum value at the 6th position in the string for those rows.
The outer query is restricted to those rows which don't match this value, so
all but the rows which do are deleted.
Ken Sheridan
Stafford, England
:
I read several posts relative to returning top N values. Perhaps I am
stupid, but I cannot make the transition to apply similar logic to return
bottom values.
I have a table with a primary key called [Key]. It contains unique values
such as: 01-02E-9432, 01-02F-9432, 10-10A-1234, 10-10B-1234, 10-10C-1234
etc. The letter in each string represents a Rev (Revision) level. There may
be many revs. I want to write a query that deletes all but the latest rev.
I know how to return the MAX value which returns the values 01-02F-9432 and
10-10C-1234 from my values above. These are the records I want to keep, the
rest need to be deleted. I cannot think of a good way to delete all but the
max value. Any ideas?