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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:47:46+00:00 2026-05-23T23:47:46+00:00

I have two tables like this: Table1ID Table2ID Table1ID SomeDate ——– —————————— 1 1

  • 0

I have two tables like this:

Table1ID               Table2ID    Table1ID  SomeDate
--------               ------------------------------
1                      1           1         2011-01-01
2                      2           1         2011-01-02
3                      3           2         2011-01-01
4                      4           3         2011-01-01
                       5           3         2011-01-02
                       6           3         2011-01-03
                       7           4         2011-01-01
                       8           4         2011-01-02

I need to insert values into a third table that give me both values from Table2, as well as a “sequence” value grouped on Table1ID. I tried this, but I’m always getting a value of 1 for the sequence.

;WITH Sequences AS (
    SELECT t1.Table1ID,
           MAX(ISNULL(t3.Sequence, 0)) AS [MaxSequence]
    FROM Table1 t1
    LEFT JOIN Table3 t3 (NOLOCK) ON t1.Table1ID = t3.Table1ID
    GROUP BY t1.Table1ID
)
INSERT INTO Table3 ( Table1ID, Table2ID, Sequence )
SELECT t1.Table1ID,
       t2.Table2ID,
       s.MaxSequence + 1
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t1.Table1ID = t2.Table2ID
JOIN Sequences s ON t2.Table1ID = s.Table1ID
ORDER BY t2.SomeDate

What I want is a result-set like this:

Table2ID    Table1ID  Sequence
------------------------------
1           1         1
2           1         2
3           2         1
4           3         1
5           3         2
6           3         3
7           4         1
8           4         2

I know I’m probably missing something stupidly simple here, but I’m stuck.

  • 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-23T23:47:47+00:00Added an answer on May 23, 2026 at 11:47 pm
    insert into Table3 (Table2ID, Table1ID, Sequence)
    select
      Table2ID,
      Table1ID,
      row_number() over(partition by Table1ID order by Table2ID)
    from Table2
    

    Something to test on:

    declare @Table2 table
    (
      Table2ID int identity,
      Table1ID int
    )
    insert into @Table2 values
    (1),(1),
    (2),
    (3),(3),(3),
    (4),(4)
    
    declare @Table3 table
    (
      Table2ID int, 
      Table1ID int, 
      Sequence int
    )
    
    insert into @Table3 (Table2ID, Table1ID, Sequence)
    select
      Table2ID,
      Table1ID,
      row_number() over(partition by Table1ID order by Table2ID)
    from @Table2
    
    select *
    from @Table3
    

    Result:

    Table2ID    Table1ID    Sequence
    ----------- ----------- -----------
    1           1           1
    2           1           2
    3           2           1
    4           3           1
    5           3           2
    6           3           3
    7           4           1
    8           4           2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables like this Member and their Purchases I need the output
I have two tables like this I would like to insert from Table1 to
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
Let's say I have two tables that look like this: TH TH TH TH
I have two tables which looks something like this Table Queue int ID; string
I have two tables like the ones below. I need to find what exchangeRate
I have two tables like this: TABLE user( id CHAR(100) text TEXT ) TABLE
I have Two tables like this: Table categories: columns: id, name, parent 1, Foods,
I have two tables like this Table Product ProductId - int <PK> ProductExpiry -
I have two already-existing tables which look (in part) roughly like this: 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.