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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:28:42+00:00 2026-05-26T06:28:42+00:00

My question is: I have two tables: table A has two columns (KeyA and

  • 0

My question is: I have two tables: table A has two columns (KeyA and Match) and table B has two columns (KeyB and Match). I want to compare with the “Match” column.

If table A has 3 rows with a particular “Match”, and table B has 2 rows, a JOIN will return me all the combinations (6 in this case). What I want it to do is match up as many as it can, and then NULL out the others.

So, it would match the first “KeyA” with the first “KeyB”, the second “KeyA” with the second “KeyB”, and then match up the third “KeyA” with NULL, since table B only has two rows for this “Match”. The order is actually irrelevant, just as long as 2 rows match up, and then one value from table A returns with a NULL for the table B value. This is not like an INNER or an OUTER JOIN.

I hope this makes sense, it was difficult to express clearly, and was hard to find keywords to search on.

EDIT:
An INNER/OUTER join would match all the table A values with all of the table B values it could. Once a B value is “used up” I do not want it to match it with any other A values.

Example:
Table A (KeyA, Match)
(1, “a”)
(2, “a”)
(3, “a”)

Table B (KeyB, Match)
(11, “a”)
(12, “a”)

Desired output (KeyA, Match, KeyB):
(1, “a”, 11)
(2, “a”, 12)
(3, “a”, NULL)

  • 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-26T06:28:42+00:00Added an answer on May 26, 2026 at 6:28 am

    You can use partition by to number the rows for each value of match. Then you can use full outer join to fill up rows per Match. For example:

    declare @A table (KeyA int, match int)
    insert @A values (1,1), (2,1), (3,1), (4,2), (5,2), (6,2)
    declare @B table (KeyB int, match int)
    insert @B values (1,1), (2,1), (3,2)
    
    select  *
    from    (
            select  row_number() over (partition by match order by KeyA) as rn
            ,       *
            from    @A
            ) as A
    full outer join
            (
            select  row_number() over (partition by match order by KeyB) as rn
            ,       *
            from    @B
            ) as B
    on      A.match = B.match
            and A.rn = B.rn
            
    

    Working code at SE Data.

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

Sidebar

Related Questions

I have two tables in a database. Table questions , has two columns, ID
I have two tables. Table #1 'color' has columns [red], [blue], [green]; where each
I have a query that joins two tables. One table has a column that
I have two tables pertaining to this question: conversations has many messages . The
I have a MySQL database with two tables (simplified for this question): movies table
I have two tables: **Product** ID Name SKU **Brand** ID Name Product table has
I have two database tables, 'Lists' and 'Notes'. Lists has columns _id, listname Notes
In MySql, I have two tables, A and B. A has as columns A.id,
I have a table called Point. The table has two columns that we'll deal
I have two tables table1 and table2 table1 has the columns: id name value

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.