Avatar of Harreni
Harreni
Flag for Saudi Arabia

asked on 

Find matched values in 2 tables having same primary key using T-SQL

Hi Experts,

I have 2 tables as below:
First table: ConsumerID (PK, nvarchar(50), DatasetID (int))
Second table: ConsumerID (PK, nvarchar(50), DatasetID (int))

and I want to have the code snippet to figure out all cases below:

- Find all DatasetID that exists in first table and HAS matched DatasetID in second table for each ConsumerID.
- Find all DatasetID that exists in first table and DOESN'T has matched DatasetID in second table for each ConsumerID.
- Find all DatasetID that exists in second table and DOESN'T has matched DatasetID in first table for each ConsumerID.
- Find all DatasetID that exists in first table UNION DatasetID in second table for each ConsumerID.

Please note that:
- ConsumerID are same for first and second table.
- Results should be one row ONLY for each consumer and resulted datasets are "separated by comma".


Thanks a lot in advance.
Harreni
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Harreni

8/22/2022 - Mon