Skip to content
Advertisement

find missing records in corresponding tables

Let’s say I have two temp tables with data that looks like this…

Here’s what I am trying to achieve

1) Exclude all records from #TEMP1 and #TEMP2 that have identical match on IdNO, CdId, ApptDate, PROC=Y

I’m only interesting in finding these scenarios:

1) Find record from #Temp1 or #Temp2 that matches on all columns, except PROC. So as an example, #Temp1 has a corresponding record in #Temp2. #Temp1 has PROC=N and #Temp2 has PROC=Y, so show the record in #TEMP1 and add a column like DESC = ‘TEMP1 record has Proc=N’

2) If #TEMP1 Or #TEMP2 does not have a corresponding record in either table, then show that record as DESC = ‘TEMP1 does not have this record), or vice versa.

3) If matching records exists in both table, but both records have PROC=N, then show both records with message ‘Temp1 record has Proc=N’ and ‘Temp2 record has Proc = N’

ALl in all the results I’m hoping to achieve are this:

In the results above

Row 1: This record exists in #Temp1 and #Temp2 but #Temp1 Proc = N

Row 2,3: This record exists in #Temp1 and #Temp2 but in both cases PROC = N

Row 4: This record exists in #Temp2 but does not exist in #Temp1

Advertisement

Answer

Hmmm . . . I think the logic you want uses a union all:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement