peter
peter

Reputation: 42192

select with start, stop and rowcount, need total

I have this oracle sql which gives me a subset of records for paging. I also need the total of records, can I combine the two queries ? The real query is much more complicated (union with where etc), I simplified it to not distract from my question.

select count(*) from table;
select outerfields.* from 
 (select row_number() over (order by id asc) r, innerfields.* from 
   ( (select * from table) innerfields)) outerfields where r between 1 and 10;

obviously something like the following doesn't work

select max(r), outerfields.* from 
  (select row_number() over (order by id asc) r, innerfields.* 
    from ( (select * from m30brief) innerfields)) outerfields where r between 1 and 10;

Upvotes: 3

Views: 168

Answers (1)

Nikola Markovinović
Nikola Markovinović

Reputation: 19356

You can add count(*) over() to outerfields derived table:

select outerfields.* 
  from 
  (
    select row_number() over (order by id asc) r, 
           count(*) over () totalRows,
           innerfields.* 
      from 
      (
        select * 
          from table
      ) innerfields
  ) outerfields
 where r between 1 and 10;

Upvotes: 1

Related Questions