How can I compare multiple datasets in SQL and select only the common rows?

Hi everyone,

I have a unique request. I must do a comparison on the db side of 2 or more result sets, and locate only the rows that have matches in other datasets being compared.

In summary, I will have for example, 3 sets as shown below:


Dataset 1:

ID  Value
1    CompareTest
2    7777
3    123
4    ABCDEF

Dataset 2:

ID  Value
1    Comparing
2    7777
3    123456
4    XYZAB

Dataset 3:

ID  Value
1    CompareTest
2    7771
3    54321
4    ABBCCD


......So from the above, I want to return ONLY "7777" and Compare Test as below:

ID    Value
1     CompareTest
2      7777


That's all I want (only the common rows amongst all the datasets that I am comparing). Surely this is possible, no? (also- I must be able to support dynamic numbers of datasets to be compared (sometimes it will be 2- sometimes it may be 20).

This is a tough problem (for me!); any help or pointers you provide are greatly, greatly appreciated.


Thanks to all in advance!
 


OddiCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SharathData EngineerCommented:
Check this query. In the inner query, i took 3 DataSets. You have to include all your Datasets in the inner query.
SELECT ID,Value FROM 
(SELECT ID,Value
  FROM DataSet1
 UNION ALL
SELECT ID,Value
  FROM DataSet2
 UNION ALL
SELECT ID,Value
------ Include all your Datasets here with UNION ALL
)
GROUP BY ID,Value
HAVING COunt(*) > 1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SharathData EngineerCommented:
I assumed that combination of ID and Value is unique in one Dataset.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.