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

  • Home
  • SEARCH
  • 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 7218157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:46+00:00 2026-05-28T21:26:46+00:00

I have a query that I always want to return 10 records for: set

  • 0

I have a query that I always want to return 10 records for:

set rowcount 10
select row_number() over(order by count(*) desc) row_num
    ,hist_report_id
    ,max(rpt_report_name) report_name
from utility.dbo.tbl_report_history
join utility.dbo.tbl_report_definitions
    on hist_report_id = rpt_report_id
where hist_user_id = 1038
group by hist_report_id

Which works fine if I have 10 or more records. The problem is when there are less than 10 records, I still need to return the rownumber field with nulls in the report_id and report_name fields.

If there were only 7 records returned, the results should look like:

row_num report_id report_name
1       id1       name1
2       id2       name2
3       id3       name3
4       id4       name4
5       id5       name5
6       id6       name6
7       id7       name7
8       null      null
9       null      null
10      null      null

Any suggestions?

I am using SQL Server 2008

  • 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. Editorial Team
    Editorial Team
    2026-05-28T21:26:47+00:00Added an answer on May 28, 2026 at 9:26 pm

    count() can never return less than zero… so just append 10 dummy rows with -1 in the count column via union

    Also, don’t use SET ROWCOUNT because it affects intermediate results

    SELECT TOP 10
       row_number() over(order by TheCount desc) AS row_num,
       hist_report_id,
       report_name
    FROM
        (
        select 
            ,count(*) AS TheCount
            ,hist_report_id
            ,max(rpt_report_name) AS report_name
        from 
           utility.dbo.tbl_report_history
           join 
           utility.dbo.tbl_report_definitions on hist_report_id = rpt_report_id
        where hist_user_id = 1038
        group by hist_report_id
        UNION ALL
        SELECT TOP 10
             -1, NULL, NULL
        FROM sys.columns
        ) T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a query that return only one row (always) and i want to
I have a query that always returns one row, with many columns. I would
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a query that returns a result set similar to the one below:
I have following query working : SELECT COUNT(id), AgeRange FROM ( select id, case
I have a query with a sub query in the select that needs to
I am using in C# MYsql .I have query that works if I run
I have a query that I know can be done using a subselect, but
I have a query that updates one record, and only one record. Is there

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.