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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:31:55+00:00 2026-05-16T23:31:55+00:00

I’m fairly new to sql and was hoping that someone can help me with

  • 0

I’m fairly new to sql and was hoping that someone can help me with an update query. I have a users table with group_id (foreign key), user_id and user_index columns. There are multiple users corresponding to each individual group_id, and user_id is a serial column which goes from 1 to the table size.

I’m looking for a query that will update the user_index column so that, for each group_id, each user will have a unique, sequential index starting with 1. So within group 1 there would be user_index 1,2,3… and within group 2 there would be user_index 1,2,3… and so on. Here is an example to clarify:

Initial state:

user_id | group_id | user_index
1         1          0
2         1          0
3         1          0
4         2          0
5         3          0
6         3          0

Desired state:

user_id | group_id | user_index
1         1          1
2         1          2
3         1          3
4         2          1
5         3          1
6         3          2

I hope that’s clear. This would be easy to do in C or C++, but I’m wondering if there’s a way to do it in sql.

  • 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-16T23:31:56+00:00Added an answer on May 16, 2026 at 11:31 pm
    UPDATE TableName
    SET user_index = (SELECT COUNT(1) FROM TableName t2 
       WHERE t2.group_id = TableName.group_id AND t2.user_id <= TableName.user_id)
    

    EDIT:
    After looking at author’s comment I created a test to see is this the right solution. Here’s the test:

    CREATE TABLE #table (user_id int, group_id int, user_index int)
    
    INSERT INTO #table VALUES (1, 1, 0)
    INSERT INTO #table VALUES (2, 1, 0)
    INSERT INTO #table VALUES (3, 1, 0)
    INSERT INTO #table VALUES (4, 2, 0)
    INSERT INTO #table VALUES (5, 3, 0)
    INSERT INTO #table VALUES (6, 3, 0)
    
    SELECT * FROM #table
    
    UPDATE #table
    SET user_index = (SELECT COUNT(1) FROM #table t2 
    WHERE t2.group_id = #table.group_id AND t2.user_id <= #table.user_id)
    
    SELECT * FROM #table
    
    DROP TABLE #table
    

    The output of two selects are exactly the same as in author’s two selects – the first as beginning state, and the second as the desired outcome.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.