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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:56:45+00:00 2026-06-17T10:56:45+00:00

I have the following query: declare @temp1 table (ID1 int not null, ID2 int

  • 0

I have the following query:

declare @temp1 table
(ID1 int not null,
 ID2 int not null)

set nocount off

insert into @temp1 values(1453,931)
insert into @temp1 values(1454,931)
insert into @temp1 values(1455,931)

insert into @temp1 values(2652,1101)
insert into @temp1 values(2653,1101)
insert into @temp1 values(2654,1101)
insert into @temp1 values(2655,1101)
insert into @temp1 values(2656,1101)

insert into @temp1 values(3196,1165)

insert into @temp1 values(3899,1288)
insert into @temp1 values(3900,1288)
insert into @temp1 values(3901,1288)
insert into @temp1 values(3902,1288)

--select * from @temp1

select ID1,ID2, ROW_NUMBER() over(partition by ID2 order by ID1) as RowNum1
from @temp1

What I now want to do is create a new column that will group all the ID2’s together..i.e ID2’s having 931 should have value 1 in the new column, 1101 should have 2, 1165 should be 3 and finally all 1288 should have 4… Can I get help for that please?

  • 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-17T10:56:46+00:00Added an answer on June 17, 2026 at 10:56 am

    You can use DENSE_RANK() to attain the result. It returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question. Refer link DENSE_RANK (Transact-SQL) for more details. Please try:

    select ID1, ID2, DENSE_RANK() over(order by ID2) as RowNum1
    from @temp1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: Declare @cnt as int Declare @int as int Set
I have the following SQL query: Declare @Total_SysDown as int, @Login_SysDown as int Set
I have the following query: DECLARE @Region VARCHAR(MAX) SET @Region = '' SELECT @Region
I have the following query: DECLARE @str VARCHAR(500) SET @str = 'Barcode: 22037 CaseSize:
I have a question about perf of the following query: DECLARE @detail_level INT =
I have the the following SQL query Declare @tempcalctbl Table ( ItemId varchar(50), ItemLocation
I have the following query DECLARE @ProductIdsToExclude nvarchar(max) SET @ProductIdsToExclude = '49506541-4CE2-40AC-812A-7AB262E6F0B0,49506541-4ce2-40ac-812a-7ab262e6f0b0' I'm then
All, I have the following dynamic SQL Query DECLARE @TableName NVARCHAR(255); SET @TableName =
I have the following query: DECLARE @sync_table_name nvarchar(500) SET @sync_table_name = 'ContactTypes' DECLARE @sync_batch_size
I have the following t-sql code which generates an error Declare @table TABLE (

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.