I have 3 tables (sya A, B and C) and they have 9 common columns (ID_number, company, Role, Seq_number, FirstName, MiddleName, LastName, Gender & DOB) between them.
Table A has 2 types of records
- Records from table B or C
- records which are not from table B & C
Now I want to compare only those records in table A which are from table B & C with the records that are available in table B & C based on those 9 common columns. There might be difference in data of one common column between the records in table A & (table B or C). I want to pick that particular record only.
Example:
Table A
ID_Number company Role Seq_Number FirstName MiddleName LastName Gender DOB system city state country
1234 12 OWN 1 Test1 mid1 last1 F 1979-01-26 ABC YYY ZZZ IND
Table B or C
ID_Number company Role Seq_Number FirstName MiddleName LastName Gender DOB System address
1234 12 OWN 1 Test1 mid1 last1 F 1990-08-30 ABC IND
In the above example, each of the common column is compared with one another. Only the column DOB differs. Hence I have to report this record. Likewise, if the data in any of this common column mismatches, I need to fetch those records.I tried left join between A, B,C with filter conditions. But it is not working as expected.
Also, one column (say type) is not available in Table A. But available in tables both B & C. I need to derive the value of that column from table B or C for table A while fetching the mismatched record. while using left join to get the mismatched record, I tried to get the 'type' column value from B or C using case/ISNULL/COALSCE (since the records in table A will be from either B or C). But I am getting only NULL values, though the column has value in B or C.
Table B & C are the base for table A. So the table A has records either from B or C. Columns (ID_Number, company, Role & seq_number) are primary key for all the 3 tables and they remain same. only the remaining fields will change. I tried as below.It is fetching records which from table A which are not available in table B or C too. But, need to fetch only the mismatched records from A available in B or C.
SELECT ALC.ID_Number ,ALC.Role ,ALC.Seq_Number ,CASE WHEN BPC.Type IS NULL THEN CPC.Type ELSE BPC.Type END --,FAT_Cust.FATCA_PolicyOwner_Type AS Client_Type ,ALC.FirstName AS First_Name ,ALC.MiddleName AS Middle_Name ,ALC.LastName AS Last_Name ,ALC.Gender ,ALC.Date_Of_Birth AS DOB FROM AL_Customer ALC LEFT JOIN BL_Customer BPC ON ALC.NAME = BPC.NAME AND ALC.Company = BPC.Company AND ALC.ROLE = BPC.ROLE AND ALC.Seq_Number = BPC.Seq_Number AND ALC.FirstName = BPC.FirstName AND ALC.MiddleName = BPC.MiddleName AND ALC.LastName = BPC.LastName AND ALC.Gender = BPC.Gender AND ALC.DOB = BPC.DOB AND ALC.Record_Active = 1 LEFT JOIN CL_Customer CPC ON ALC.NAME = CPC.NAME AND ALC.Company = CPC.Company AND ALC.ROLE = CPC.ROLE AND ALC.Seq_Number = CPC.Seq_Number AND ALC.FirstName = CPC.FirstName AND ALC.MiddleName = CPC.MiddleName AND ALC.LastName = CPC.LastName AND ALC.Gender = CPC.Gender AND ALC.DOB = CPC.DOB AND ALC.Record_Active = 1 WHERE ALC.Record_Active = 1 AND (BPC.NAME IS NULL AND CPC.NAME IS NULL) AND (BPC.ROLE IS NULL AND CPC.ROLE IS NULL) AND (BPC.Seq_Number IS NULL AND CPC.Seq_Number IS NULL ) AND (BPC.FirstName IS NULL AND CPC.FirstName IS NULL) AND (BPC.MiddleName IS NULL AND CPC.MiddleName IS NULL) AND (BPC.LastName IS NULL AND CPC.LastName IS NULL) AND (BPC.Gender IS NULL AND CPC.Gender IS NULL) AND (BPC.DOB IS NULL AND CPC.DOB IS NULL) AND ALC. Record_Active =1 AND (ALC.UpdatedDate BETWEEN DATEADD(DAY, -7, GETDATE()) AND GETDATE())
Aucun commentaire:
Enregistrer un commentaire