Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 173551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:21:49+00:00 2026-05-11T13:21:49+00:00

I have the following stored procedure which returns A , B , and the

  • 0

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; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:21:50+00:00Added an answer on May 11, 2026 at 1:21 pm
    WITH q AS         (         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         ) SELECT  q.*, ROW_NUMBER() OVER (ORDER BY cnt DESC) AS rn FROM    q ORDER BY rn DESC 

    To retrieve first 10 rows, use:

    WITH q AS         (         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         ) SELECT  TOP 10 q.*,          ROW_NUMBER() OVER (ORDER BY cnt DESC, A, B) AS rn FROM    q ORDER BY cnt DESC, A, B 

    To retrieve rows between 11 and 20, use:

    SELECT  * FROM    (         WITH q AS                 (                 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                 )         SELECT  q.*,                  ROW_NUMBER() OVER (ORDER BY cnt DESC, A, B) AS rn         FROM    q         ) qq WHERE rn BETWEEN 11 AND 20 ORDER BY cnt DESC, A, B 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following stored procedure, which returns 0 results but if the run
I have a stored procedure which returns multiple result sets similiar to the following:
I have the following stored procedure which will generate mon to sun and then
I have a mysql stored procedure which is giving me the following error:- #1064
I have the following iBatis mapping for an Oracle Stored Procedure that returns a
I have the following statement in a stored procedure: DECLARE @Count INT EXEC @Count
i have the following stored procedure that returns the sent emails of the latest
I have a data access layer which returns DataSets/DataTables by executing Stored Procedure. Everything
Using a stored procedure i have a fairly complex SQL statement which returns a
I have the following stored procedure which takes a user ID, a starting date,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.