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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:21:36+00:00 2026-05-29T05:21:36+00:00

I have an issue similar to Select incremented integer but I’m trying to take

  • 0

I have an issue similar to Select incremented integer but I’m trying to take it a step further.

I have a table (a history table that tracks changes to some code) that has, among other things, an ID (id), a modified timestamp (modified), and a foreign key from another table (company_id). If I specify a single company_id, I can get my query to increment a value numbering from 1 to X which order the items happened, but it’s only for the specific company_id.

So this…

    SELECT id, (@rev := @rev+1) AS revision, company_id
    FROM code_history AS ch
    INNER JOIN (SELECT @rev := 0) AS r
    WHERE company_id = '1'

Returns…

    ID | revision | company_id
     1 |    1     |     1
     3 |    2     |     1
     4 |    3     |     1
     7 |    4     |     1
     9 |    5     |     1
    12 |    6     |     1

What I don’t know is how to make it so that each unique company_id has it’s own incremented list of ID’s and revision counts without making a query to the database for each company_id, or even if it’s possible.

Data I’d Like…

    ID | revision | company_id
     1 |    1     |     1
     3 |    2     |     1
     4 |    3     |     1
     7 |    4     |     1
     9 |    5     |     1
    12 |    6     |     1
     2 |    1     |     2
     8 |    2     |     2
    13 |    3     |     2
    18 |    4     |     2
    19 |    5     |     2
    21 |    6     |     2
     5 |    1     |     3
     6 |    2     |     3
    10 |    3     |     3
    11 |    4     |     3
    14 |    5     |     3

I appreciate any and all help, thank you!

  • 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-29T05:21:36+00:00Added an answer on May 29, 2026 at 5:21 am

    You were on the right track, you would just need a second variable to preserve what the LAST company ID was, then reset the counter back to 1

    SELECT 
          PreSortedRevisions.id, 
          @rev := if( @lastCompany = PreSortedRevisions.company_id, @rev+1, 1 ) AS revision, 
          @lastCompany := company_id as Company_ID
       FROM 
          ( select ch.company_id,
                   ch.id 
                from code_history AS ch
                order by ch.company_id,
                         ch.id ) PreSortedRevisions,
          (SELECT @rev := 0, @lastCompany := 0) AS SqlVars
       order by
          Company_id,
          Revision
    

    First, the inner query pre-gathers the records and sorts them by company and ID so they are returned in proper sequence per company.

    Then, by testing the IF() before the @LastCompany := company_id assignment, the @LastCompany starts with 0, and will be different the first time through starting the @rev = 1. After the first record, the @LastCompany will now be whatever the last one was, be the same on record 2 and increase the counter. When the company actually changes, it resets the counter back to 1.

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

Sidebar

Related Questions

my issue lokks similar to this one: (link) but i have one-to-many association: <set
I have a report table that looks similar to this reports inspection_type | inspection_number
Before anyone jumps on me, I have found a similar issue here , but
have created a map that I'm trying to have function similar to 'My Maps'.
I have a similar issue like here: http://social.msdn.microsoft.com/forums/en-US/biztalkgeneral/thread/87d5a6ec-04ee-4c6f-8267-f526ee105f0b I have an asp.net web page
I have a local site (IIS6, although similar issue with a totally different site
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that is driving me a bit nuts: Using a UserProfileManager
I have noticed when I am trying to implement the dayClick: and the select
I have an SOAP response that looks similar to this: <?xml version=1.0 encoding=UTF-8?> <soapenv:Envelope

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.