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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:10:56+00:00 2026-06-10T19:10:56+00:00

A table has a values as below, same number can be repeated in any

  • 0

A table has a values as below, same number can be repeated in any order. 100,102,101 are repeated value, for these value i need to generate a same number only if they are consecutive values.

100
101
102
100
100
103
104
102
102
105
106
101
101

Out put Should be

100    1
101    2
102    3
100    4
100    4
103    5
104    6
102    7
102    7
105    8
106    9
101    10
101    10

Please help for the query

  • 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-10T19:10:57+00:00Added an answer on June 10, 2026 at 7:10 pm

    Here your query:

    SET @t1=0;
    SET @tp=-1;
    
    select   
      @t1 := @t1 + (case when @tp=n then 0 else 1 end) as c, 
      n,
      @tp := n
    from nums
    order by n;
    

    Check it at: http://sqlfiddle.com/#!2/b136e/9/2

    Explanation

    Do you need two variables. First one is the counter. Seccond var is to remember previus value.
    For each row you show counter. You increment counter when previus value is not equal than row value.

    Testing:

    create table nums( n int );
    insert into nums values (100), (101), (101), (102);
    

    Results:

    | C |   N | @TP := N |
    ----------------------
    | 1 | 100 |      100 |
    | 2 | 101 |      101 |
    | 2 | 101 |      101 |
    | 3 | 102 |      102 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of many values where one column has the WO Number,
My table has values like ( RowCount is generated by the query below): ID
I have a table which has essentially boolean values in a legacy database. The
I have a data table which already has some values, plus it is getting
I want to consolidate values in my table using SELECT Query. My table has
I have a MySQL table that has a field of comma separated values. How
I have a DB table that already has an existing value that I don't
Wordpress, for example, has a table with name => value rows for storing application-wide
PROFESSIONAL Table has many columns FIRST_NAME,MIDDLE_NAME, LAST_NAME, TITLE_CODE, COMMUNICATION_TYPE_CODE, COMMUNICATION_VALUE PROFESSIONAL Table COMMUNICATION_TYPE_CODE column
Table has the following info: date |vendor|sales|percent| -------------------------- 2009-03-03| 10 |13.50| 1.30 | 2009-03-10|

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.