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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:46:42+00:00 2026-06-16T18:46:42+00:00

I want to select 2 columns from a table, and assign a int value

  • 0

I want to select 2 columns from a table, and assign a int value to each value. However, I want the 1st column ID to be the same for all values that are the same.

For the 2nd column, I want each value to numbered as well, but partitioned by the first column. I have figured this piece out, but I can’t get the first part to work.

Here is the test scenario I’m using.

DECLARE @TestTable as Table (Column1 char(1), Column2 char(1))

INSERT INTO @TestTable SELECT 'A','A' 
INSERT INTO @TestTable SELECT 'A','B' 
INSERT INTO @TestTable SELECT 'A','C' 
INSERT INTO @TestTable SELECT 'B','D' 
INSERT INTO @TestTable SELECT 'B','E' 
INSERT INTO @TestTable SELECT 'B','F' 
INSERT INTO @TestTable SELECT 'B','G' 
INSERT INTO @TestTable SELECT 'B','H' 
INSERT INTO @TestTable SELECT 'C','A' 
INSERT INTO @TestTable SELECT 'C','B' 
INSERT INTO @TestTable SELECT 'C','C' 


SELECT 
    Row_Number() OVER (Partition BY Column1 ORDER BY Column1) as Column1_ID,
    Column1,
    Row_Number() OVER (Partition BY Column1 ORDER BY Column1, Column2) as Column2_ID,
    Column2
FROM @TestTable

When I run this, the values in Column2_ID are correct, but I would like the values for Column1_ID to be as follows.

Column1_ID  Column1  Column2_ID  Column2
1             A        1           A
1             A        2           B
1             A        3           C
2             B        1           D
2             B        2           E
2             B        3           F
2             B        4           G
2             B        5           H
3             C        1           A
3             C        2           B
3             C        3           C
  • 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-16T18:46:43+00:00Added an answer on June 16, 2026 at 6:46 pm

    You just need to use a different ranking function,

    dense_rank() OVER (ORDER BY Column1) as Column1_ID
    

    http://msdn.microsoft.com/en-us/library/ms173825.aspx

    SQL Fiddle : http://www.sqlfiddle.com/#!6/d41d8/1832

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

Sidebar

Related Questions

Put simply, I want to select all data in two columns from each of
I want to select columns from a table where the values of columns are
I want to select rows from a table if a column contains a certain
Table A, columns OrderId, OrderTimeStamp (datetime). I want to SELECT all records for any
I want to select 15 columns from a table of 20 columns. Is there
I want to select two columns from the table so is it possible to
There is a MySQL database and I want to select columns from a table.
I have a table called Movies i want to select some columns from it
I would like my users to select the columns of table they want to
I have table with 3 columns A B C. I want to select *

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.