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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:05:41+00:00 2026-05-21T16:05:41+00:00

Using this sample dataset: CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT,

  • 0

Using this sample dataset:

CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT, CCC VARCHAR (12));
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A123', 45, 123, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A120', 52, 120, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A133', 63, 133, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D123', 34, 123, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D120' ,32, 120, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D140', 12, 140, '2011-04' ); 

Im look for table with 3 column.

Col A “Id” order as Desc from orginal.

Col B “Id2” as previous or next row Id in col A where CCC are the same.

Id,  Id2  CCC
A120 A123 '2011-03'
A123 A133 '2011-03'
A133      '2011-03'
D120 D123 '2011-04'
D123 D140 '2011-04'
D140      '2011-04'

or

  Id,  Id2  CCC
A120      '2011-03'
A123 A120 '2011-03'
A133 A123 '2011-03'
D120      '2011-04'
D123 D120 '2011-04'
D140 D123 '2011-04'

OR

   Id,  Id2  CCC
   A123 A120 '2011-03'
   A133 A123 '2011-03'
   D123 D120 '2011-04'
   D140 D123 '2011-04'

Could I add an autocrement col to joined table, then use 1, 2, 3 rows up or down? The id2 would then be based on this autocrement row?

  • 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-21T16:05:42+00:00Added an answer on May 21, 2026 at 4:05 pm

    Operating on Marc’s basis:

    SELECT test.id, child.id, test.CCC
    FROM test
      LEFT JOIN test AS child
        ON (test.CCC = child.CCC)
          AND (test.id < child.id)
    WHERE NOT EXISTS
      ( SELECT 1
        FROM test AS middle
        WHERE (test.CCC = middle.CCC)
          AND (test.id < middle.id)
          AND (middle.id < child.id)
      )
      OR child.id IS NULL
    ORDER BY test.id
    

    This might be helpful with more complex queries:

    CREATE VIEW testWithRowId AS
      ( SELECT test.id
             , COUNT(test.id) AS rownum
             , test.CCC
        FROM test
          JOIN test AS child
            ON (test.CCC = child.CCC)
              AND (test.id >= child.id)
        GROUP BY test.CCC
               , test.id 
      )
    

    and then use it:

    SELECT t1.id
         , t2.id AS idShifted
         , t1.CCC
    FROM testWithRowId t1
      LEFT JOIN testWithRowId t2
        ON (t2.CCC = t1.CCC) 
          AND (t2.rownum = t1.rownum + 1)   ---- replace this 1 with 2 or 3, etc
    ORDER BY t1.CCC                         ---- for a shift 2 or shift 3, etc
           , t1.rownum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this simple regular expression to validate a hex string: ^[A-Fa-f0-9]{16}$ As you
I've written this very simple function to replace a file extension using LINQ in
Is it a simple case of just never using the this.XYZ construct?
I know this is a darn simple question, but I'm very used to using
Using this question as the base is there an alogrithm or coding example to
Since I have started using this site, I keep hearing about the Boost library.
I've been using this nifty LINQ to SQL tool for a data access layer
anyone have any experience using this? if so, is it worth while?
I'm using this code, to make a request to a given URL: private static
Should I be using this method of throwing errors: if (isset($this->dbfields[$var])) { return $this->dbfields[$var];

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.