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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:03:30+00:00 2026-06-16T16:03:30+00:00

I have 2 table with below structure : Table 1 (Tb1) : MasId Int,

  • 0

I have 2 table with below structure :

Table 1 (Tb1) :

MasId Int, ItemId int, Quantity int
1          1           10
1          2           5
1          3           18
2          2           3
2          4           8

Table 2 (Tb2) :

MasId Int, ItemId int, Quantity int
10          1           10
10          2           5
10          3           18
6           2           3
6           4           8

I want to match Tb1 with Tb2 with ItemId and Quantity Columns. and get below query :

Tb1.MasId   Tb2.MasId
1           10
2           6

Table1 and Table2 contain about 2000000 record. and I use SQL Server 2012

EDIT 1

In Tb1 and Tb2 exists records such below :

Tb1:

MasId, ItemId, Quantity
3      15       10
3      16       2

Tb2:

MasId, ItemId, Quantity
20     15      10
20     18      5

Tb1.MasId and Tb2.MasId is not matched together.

  • 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-06-16T16:03:31+00:00Added an answer on June 16, 2026 at 4:03 pm

    The following query available as a SqlFiddle returns correct results even with tricky data:

    SELECT
       Tb1MasId = A1.MasId,
       Tb2MasId = B1.MasId
    FROM
       (
          SELECT
             MasID,
             Pattern = Checksum_Agg(Binary_Checksum(ItemId, Quantity))
          FROM dbo.Tb1
          GROUP BY MasId
       ) A1
       INNER JOIN (
          SELECT
             MasID,
             Pattern = Checksum_Agg(Binary_Checksum(ItemId, Quantity))
          FROM dbo.Tb2
          GROUP BY MasId
       ) B1
          ON A1.Pattern = B1.Pattern
          AND NOT EXISTS (
             SELECT * 
             FROM
                (
                   SELECT A2.ItemID, A2.Quantity
                   FROM dbo.Tb1 A2
                   WHERE A1.MasID = A2.MasID
                   UNION ALL
                   SELECT B2.ItemID, B2.Quantity
                   FROM dbo.Tb2 B2
                   WHERE B1.MasId = B2.MasID
                ) X
             GROUP BY ItemID, Quantity
             HAVING Count(*) = 1
          );
    

    The other answers on the page give incorrect results with the sample data in my SqlFiddle. Specifically, adding the following data to Tb2 will break X.L.Ant’s current query (which will show a false match 2, 8 due to not detecting that the 8, 3, 18 doesn’t belong).

    INSERT dbo.Tb2
    VALUES
       (7, 1, 10),
       (7, 2, 5),
       (7, 3, 17),
       (8, 3, 18),
       (8, 2, 3),
       (8, 4, 8);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below table structure ID int xmlData xml I want data in the
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a table with below structure. I am not having control over changing
I have a mysql database with a table structure like below : Table Name
I have a table structure having a EMP_DATE column as below ID EMP_DATE ----
I have two table masterTbl and detailTbl which structure is given below... --PRIMARY TABLE
I have a table T (structure below) which initially contains all-NULL values in an
I have a table structure like below : categoryID bigint , primary key ,
I have below table structure in MS SQL AirQuoteID Name SalesValue 7 M 49.50
Would like to get the following as a result from the table structure below

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.