Records m to n are the records 1 to n minus the records 1 to m-1. Be aware though that you need an ORDER BY
clause for a TOP
clause to make sense.
Here is an example with m = 31 to n = 40 and an order by all three selected columns. MS Access does not support EXCEPT
so we cannot subtract the two data sets, which would be the straight-forward way to go. We could also express the desired result as the top n where (a,b,c) not in top m-1, but MS Access does not support an IN
clause on multiple columns either. So I am using an anti join here (for which I select dist_ft
, but it can be any non-nullable column of the table).
In case your table has a unique ID column, you can use a more readable where (id) not in (select top 30 id ...)
instead of an anti join. In any way make sure to apply the same WHERE
clause (dist_ft < 3001
in your case) and ORDER BY
clause (e.g. ORDER BY a, b, c
) to the main query and subquery.
SELECT TOP 40 a, b, c
FROM table_name t
LEFT JOIN
(
SELECT TOP 30 a, b, c, dist_ft
FROM table_name
WHERE dist_ft < 3001
ORDER BY a, b, c
) no ON no.a = t.a AND no.b = t.b AND no.c = t.c
WHERE t.dist_ft < 3001
AND no.dist_ft is null
ORDER BY t.a, t.b, t.c;
MS Access is known for requiring additional parentheses on multiple joins. I cannot say whether above query works straight away or if parantheses must be added somewhere.