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 7730777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:18:41+00:00 2026-06-01T06:18:41+00:00

I have a query that returns data with group category and some details like

  • 0

I have a query that returns data with group category and some details like this:

Category | Title
==================
cat1  ---  titlex
cat1  ---  titley
cat2  ---  titley
cat3  ---  titlez
cat3  ---  titlex
cat4  ---  titlex

I want to display a table that has row number on outer group (Category) like this:

RN | Category | Title
======================
1    cat1  
                titlex
                titley
2    cat2
                titley
3    cat3
                titlez
                titlex
4    cat4 
                titlex

The problem is, when I add RN column as ROW_NUMBER in sql query or ROWNUMBER SSRS function (tried NOTHING, Group and Details as a scope, just in case), I always get numbers like 2 1 2 or 1 3 4 6 for RN column.

EDIT
Sql Query (table names and properties changed for simplicity)

SELECT    
        -- this rownumber does not work, counts every occurrence of category    
    --ROW_NUMBER() OVER (
        --PARTITION BY c.Name -- tried this too, this resets on each cat
        --ORDER BY c.Name) AS RN,
    c.Name, 
    p.Name
FROM
    Products p INNER JOIN
    Categories c ON p.CategoryId = c.Id
GROUP BY c.Name, p.Name
ORDER BY c.Name, p.Name
  • 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-06-01T06:18:43+00:00Added an answer on June 1, 2026 at 6:18 am

    You don’t want the row numbers (as you’ve observed, the row numbers are assigned to every… um… row).

    Maybe you want DENSE_RANK?

    SELECT    
        DENSE_RANK() OVER (ORDER BY c.Name) AS RN,
        c.Name, 
        p.Name
    FROM
        Products p INNER JOIN
        Categories c ON p.CategoryId = c.Id
    GROUP BY c.Name, p.Name
    ORDER BY c.Name, p.Name
    

    As to your desired output, I wouldn’t attempt to achieve that in SQL – use a reporting/formatting tool to get the final layout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Microsoft SQL Server 2008 query that returns data from three tables
In my web application, I have a dynamic query that returns huge data to
I have a query that returns a lot of data into a CSV file.
Say I have a stored procedure that returns data from a SELECT query. I
I have a YQL query that extracts data from a page and returns it
I have a query in Oracle for a report that looks like this: SELECT
I have this rather complex query that grabs data from three tables, and now
Good Day, I have a sql query that returns the following data: EmployeeID...Employee 555..........John
I have a query that returns a result set similar to the one below:
I have a query that returns a string, as well as an escape character

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.