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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:07:34+00:00 2026-06-12T16:07:34+00:00

I have a resultset in a temp table on SQL Server 2008. One of

  • 0

I have a resultset in a temp table on SQL Server 2008. One of the fields is a group Id, and for each group I can have any number of rows. I’d like to get in a new column the position of the row in the group, such as “row 1 of 3”
So, if I have temp table #Data(GroupId int, Item char) with

GroupId  Item
-------------
   5      C
   5      A
   5      T
   2      U
   3      Y
   3      L

I’d like to get

GroupId  Item Pos GroupCount
----------------------------
   5      C    1     3
   5      A    2     3
   5      T    3     3
   2      U    1     1
   3      Y    1     2
   3      L    1     2

I managed to do this doing

SELECT D.GroupId, D.Item, ROW_NUMBER() OVER (PARTITION BY D.GroupId ORDER BY D.Item) AS Pos, COUNT(1) AS GroupCount
  FROM #Data D
       INNER JOIN #Data DC
 GROUP BY D.GroupId, D.Item

But I’m not too happy with this, because the query is actually a little more complex because of all the columns I actually have to retrieve, and because doing both an inner join with itself to get the group row count and a partition to get the row position seems like too much.
Is there a simpler way to do this?


I didn’t exactly use Tim’s answer, but it showed me the way:

SELECT D.GroupId, D.Item,
       ROW_NUMBER() OVER (PARTITION BY D.GroupId ORDER BY D.Item) AS Pos,
       COUNT(D.GroupId) OVER (PARTITION BY D.GroupId) AS GroupCount
  FROM #Data D
  • 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-12T16:07:35+00:00Added an answer on June 12, 2026 at 4:07 pm

    You can use COUNT(GroupID)OVER(PARTITION BY GroupID):

    WITH CTE AS
    (
      SELECT GroupId, Item
      , GroupCount = COUNT(GroupID) OVER (PARTITION BY GroupID)
      , Pos = ROW_NUMBER() OVER (Partition By GroupID Order By GroupId)
      FROM #Data
    )
    SELECT GroupID, Item, Pos, GroupCount 
    FROM CTE
    

    OVER Clause (Transact-SQL)

    Here’s a fiddle: http://sqlfiddle.com/#!6/b6505/2/0

    Note that it’s not ordered since you haven’t told how to order.

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

Sidebar

Related Questions

I have 2 tables I'm trying to join in SQL Server . Table one
I have a ResultSet object containing all the rows returned from an sql query.
I have the following situation: .net 3.5 WinForm client app accessing SQL Server 2008
Using SqlServer 2008, I have a temp table (#tmpAllSales) that captures all sales for
Using SQL Server 2008 and any available features of TSQL, I am trying to
I have a SelectSP which returns resultset that i am storing in the temp
Given that I have this resultset structure (superfluous fields have been stripped) Id |
I'm using jdbcTemplate, and have a ResultSet consisting of about 100 named fields. And
I have a java.sql.ResultSet object containg data from a query that was run. How
I have a table from which I query data to display resultset using stored

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.