I have the following stored procedure which returns A, B, and the count in descending order. I am trying to use ROW_NUMBER, so I can page the records, but I want the first row number 1 to be the record with the highest count, so basically, if I return a table with 3 records and the count is 30, 20, 10, then row number 1 should correspond with count 30, row number 2 should correspond with count 20, and row number 3 should correspond with count 10. dbo.f_GetCount is a function that returns a count.
create procedure dbo.Test as @A nvarchar(300) = NULL, @B nvarchar(10) = NULL as select @A = nullif(@A,'') ,@B = nullif(@B,''); select h.A ,hrl.B ,dbo.f_GetCount(hrl.A,h.B) as cnt from dbo.hrl inner join dbo.h on h.C = hrl.C where(@A is null or h.A like '%'+@A+'%' ) and (@B is null or hrl.B = @B ) group by hrl.B ,h.A order by cnt desc;
To retrieve first
10rows, use:To retrieve rows between
11and20, use: