Row Counts and Position information within the set
751883Feb 5 2010 — edited Feb 8 2010We execute SQL statements that could either end up with a sparse or dense collection of records out of the tables involved, but we typically only retrieve the records in small groups by limiting the query. A typical statement may be similar to:
SELECT * FROM (
SELECT ROW_NUMBER() OVER (
ORDER BY P.SITE_NAME, P.SURFACE_LOCATION, P.XPRIME)
AS X_ROWNUMBER, P.XPRIME, P.SITE_NAME, P.SURFACE_LOCATION
FROM PAD_SITE P
WHERE P.XSITE=:master
ORDER BY P.SITE_NAME, P.SURFACE_LOCATION, P.XPRIME)
WHERE X_ROWNUMBER <= 20
For the next group, we execute a similar query, but with a different range on the X_ROWNUMBER
We display the result in a web page, but need to know how many ‘pages’ of records, and where to position the scrollbar to represent where within the set a given record is, as if we had executed the query as:
SELECT P.XPRIME, P.SITE_NAME, P.SURFACE_LOCATION
FROM PAD_SITE P
WHERE P.XSITE=:master
ORDER BY P.SITE_NAME, P.SURFACE_LOCATION, P.XPRIME
Basically, given a key XPRIME, what is its row location out of a total number of rows in the query above.
These queries may be working with result sets that number in the 10’s of thousands of rows.
Is there a fast way for us to know how many records are in the entire set, and what position within the set, respecting the sort order, any given record occupies?
The solution wouldn't have to be limited to q SQL statement, it could involve stored procs, index tricks, temp tables, etc., it just has to be fast, and not degrade rapidly as the number of rows climbs.