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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:06:15+00:00 2026-05-31T23:06:15+00:00

Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE

  • 0

Given the following data/schema:

DECLARE @t1 TABLE (
    Id int NOT NULL
)

DECLARE @t2 TABLE (
    Id INT NOT NULL,
    Val1 CHAR(1) NOT NULL
)

DECLARE @t3 TABLE (
    Id INT NOT NULL,
    Val2 CHAR(1) NOT NULL
)

INSERT INTO @t1 VALUES (1)
INSERT INTO @t2 VALUES (1, 'A')
INSERT INTO @t2 VALUES (1, 'B')
INSERT INTO @t2 VALUES (1, 'C')
INSERT INTO @t3 VALUES (1, '9')
INSERT INTO @t3 VALUES (1, '8')

I would like to see the following result set:

Id,Val1,Val2
1,'A','9'
1,'B','8'
1,'C',NULL

Now add the following:

INSERT INTO @t3 VALUES (1, '7')
INSERT INTO @t3 VALUES (1, '6')

And I would like to see this:

Id,Val1,Val2
1,'A','9'
1,'B','8'
1,'C','7'
1,NULL,'6'

Here’s what I came up with:

DECLARE @t4 TABLE(
    Id INT NOT NULL,
    RowNumber INT NOT NULL,
    Val1 CHAR(1) NULL,
    Val2 CHAR(1) NULL
)

INSERT INTO @t4 (Id, RowNumber, Val1)
    SELECT Id, ROW_NUMBER() OVER(ORDER BY Id), Val1
        FROM @t2

MERGE @t4 T
USING(SELECT Id, ROW_NUMBER() OVER(ORDER BY Id) RowNumber, Val2 FROM @t3) S
ON (T.Id = S.Id AND T.RowNumber = S.RowNumber)
WHEN MATCHED THEN
    UPDATE SET T.Val2 = S.Val2
WHEN NOT MATCHED THEN
    INSERT (Id, RowNumber, Val2) VALUES (Id, RowNumber, Val2);

SELECT Id, Val1, Val2 FROM @t4

But I’d like to know if someone can come up with something simpler, using SELECT with JOINs instead MERGE

  • 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-31T23:06:16+00:00Added an answer on May 31, 2026 at 11:06 pm
    ;with q2 as (
        select
            Id,
            Val1,
            ROW_NUMBER() OVER(ORDER BY Val1) rn
        from @t2
    ), q3 as (
        select
            Id,
            Val2,
            ROW_NUMBER() OVER(ORDER BY Val2 DESC) rn
        from @t3
    )
    select 
        isnull(q2.Id, q3.Id) as Id, 
        q2.Val1, 
        q3.Val2
    from 
        q2 full outer join
        q3 on q2.Id = q3.Id and q2.rn = q3.rn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following schema / data / output how would I format a SQL
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a SQL table with the following schema: fruit_id INT price FLOAT date
I have the following DB Schema :- Data is ... Location Table 1. New
I have an odd linq subquery issue. Given the following data structure: Parents Children
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given the following simple BST definition: data Tree x = Empty | Leaf x
How can you sort the following word by the given rule? Example data is
Hey guys, given a data set in plain text such as the following: ==Events==
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(

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.