I want to use this SQL query to get only the records between 8 and 10:
select *
from(
SELECT a.*,rownum rn
FROM ACTIVESESSIONSLOG a
ORDER BY USERID ASC)
WHERE rn >= 8 and rn <= 10
When I implement this SQL query into pagination I get every time 1 row on the second page no matter how many rows I have configured to be displayed into the pages. Is this SQL query valid?
This is the table structure:
-- TABLE ACTIVESESSIONSLOG
CREATE TABLE ACTIVESESSIONSLOG(
ASESSIONID VARCHAR2(30 ) NOT NULL,
USERID VARCHAR2(30 ),
ACTIVITYSTART TIMESTAMP(6),
ACTIVITYEND TIMESTAMP(6),
ACTIVITY CLOB
)
/
Best wishes
rownumis applied before theORDER BYso your query is almost certainly not doing what you expect. Your query is essentially asking for an arbitrary 3 rows and theORDER BYisn’t doing anything useful.You could use the analytic function
row_numberinstead, i.e.which will page through the results
It may be more efficient, however, to do something like this where Oracle can use the inner
rownum <= 10predicate to know that it can stop sorting the data once it has identified the first 10 rows.