It’s made a little more complex as its all but the top 1 per subset of
rows
(as defined by the key value excluding the revision value) which are to
be
deleted. As you'll see from my reply this can be done by means of a
subquery
which restricts the outer query. In this case as it’s the top 1 per
group
which are to remain the MAX operator can be used. If it were a
constant
number of rows greater than 1 per group to be retained then the TOP
option
could be used in the subquery, which again would be correlated with the
outer
query, and sorted in DESC order. In this case a NOT IN operation would
be
undertaken on the subquery rather than the inequality operation in my
reply.
If portability is an issue it could also be done without having to
resort
to
the non-standard TOP option by using the COUNT operator to identify the
relevant rows to be retained, i.e. delete those where the count of rows
returned by a subquery correlated on the unrevised key values being the
same
and the revision value being <= that in the outer query's current row
is
more
than 1.
Ken Sheridan
Stafford, England
:
I think the problem here is that the user wants to delete everything
except
the top 10. If there are 25 records, deleting the bottom 10 will
leave
15.
Same idea applies to viewing everything except the top 10. In VBA I
would
try getting the record count minus 10, then use that result for the
TOP
number, with the records sorted DESC, but I do not see how to do that
directly in a query. I expect it can be done; I just don't know how.
To the OP, why delete the records? Access can handle millions of
properly
indexed records. Why not just view the top 10?
Ok, this is just a conceptual thing. Let's say you want to see all
sales rep statistics for the month of January, 2009. The query
would look like this
SELECT RepName, SalesTotal
FROM qryOrders
WHERE OrderDate BETWEEN #1/1/09# AND #1/31/09#
ORDER BY SalesTotal ASC
What if you wanted to see the the 10 HIGHEST sales results?
Add TOP 10 to the select and sort Descending.
SELECT TOP 10 RepName, SalesTotal
FROM qryOrders
WHERE OrderDate BETWEEN #1/1/09# AND #1/31/09#
ORDER BY SalesTotal DESC
How would you show the 10 LOWEST sales results? Same
query, same TOP 10, but sort on Sales Totals Ascending.
Clear as mud?
--
Danny J Lesandrini
(e-mail address removed)
www.amazecreations.com
Then what? I don't want to manaully delete these records, I want
to
automate
it. I don't think the sort order is going to help me select
records
to
delete automatically. (I could do it manually no matter what order
it
was
sorted!)
:
Reverse the sort order.
--
Danny J Lesandrini
(e-mail address removed)
www.amazecreations.com
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?