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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:48:40+00:00 2026-06-17T20:48:40+00:00

So I have this table where I only want to look at AB. ID

  • 0

So I have this table where I only want to look at AB.

ID     CODE       COUNT   
102    AB         9
101    AB         8
100    AC         23    //not important!!!!
99     AB         7
98     AB         6
97     AB         5
96     AB         0

I want this table to look like this

ID    NEWID     CODE       COUNT   
102   102       AB         9
101   101       AB         8
99    100       AB         7
98    99        AB         6
97    98        AB         5
96    97        AB         0

How do I get a new ID which counts consecutively where the code is AB?

This is then used for a SELECT query, the initial table should stay intact, so this is not UPDATING the table but just making a fictive NEWID.

  • 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-17T20:48:41+00:00Added an answer on June 17, 2026 at 8:48 pm

    You can use a user variable to create a counter to accomplish this (in just one query!):

    SELECT t.ID, @NEWID := COALESCE(@NEWID - 1, t.ID) AS NEWID, t.CODE, t.COUNT
    FROM
        (SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
        (SELECT @NEWID := NULL) _uv;
    
    mysql> SELECT t.ID, @NEWID := COALESCE(@NEWID - 1, t.ID) AS NEWID, t.CODE, t.COUNT
        -> FROM
        ->     (SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY I
    D DESC) t,                                                                        
        ->     (SELECT @NEWID := NULL) _uv;
    +------+-------+------+-------+
    | ID   | NEWID | CODE | COUNT |
    +------+-------+------+-------+
    |  102 |   102 | AB   |     9 |
    |  101 |   101 | AB   |     8 |
    |   99 |   100 | AB   |     7 |
    |   98 |    99 | AB   |     6 |
    |   97 |    98 | AB   |     5 |
    |   96 |    97 | AB   |     0 |
    +------+-------+------+-------+
    6 rows in set (0.03 sec)
    

    Edit: Here’s an SQL Fiddle for it. (What a cool tool. I had no idea this existed!)

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

Sidebar

Related Questions

I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have this table And my goals is to have a table looking like
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have two tables in a teradata database that look like this accounts account_number
I have a table called dbo.Tbl_ActivityInformations and the data look like Activityid activitymaxcount Activityusedcount
I have a Standard table, which sotres parent, child category relationship...like this. id, parent,
I have this table Test as Id, SomeValue, SomeText contains about a mill records,
I have this table: UNIQUE_ID | WINNER_ID | FINALIST_ID ________1 | ________1 | __________2
I have this table structure on a SQL Server 2008 R2 database: CREATE 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.