Using SQL Server, I am querying a list of people and photos taken from one system. There can be multiple photos of the same person. The attributes are the first name, last name, date of birth,SSN, etc...
SELECT *
FROM MASTERPHOTOSDB
Output:
Master | PhotoPath | AQUIRE_TM | LastName | FirstName | MiddleName | DOB | DL
471578 | PHOTO72044 |1899-12-30 | PEREZ | JOSE | C |4/15/2000 |TX12345678
468893 | PHOTO72049 |1899-12-30 | JOHNSON | TRENT | | |TX87654321
56553 | PHOTO72055 |1899-12-30 | SMITH | JOHN | D |1/1/1990 |TX11223344
56553 | PHOTO72056 |1899-12-30 | SMITH | JOHN | D |1/1/1990 |TX11223344
With that, I am trying to search another system for the same person based on certain criteria as a match:
- last name, first name, middle name, DOB, DL (if not match go to 2)
- last name, first name, DOB, DL (if no match go to 3)
- Last name, first name, SSN, etc (if no match end search, go to next photo)
Below is a piece of the query that looks for matches with an IF statement(just a sample of the entire query):
IF (SELECT TOP (1) COUNT(*)
FROM [InformHelper].[dbo].[InformMasterNamesTST]
WHERE [LastName] = 'SMITH'
AND [FirstName] = 'JOHN'
AND DOB = '01/01/1990'
AND DL = 'TX12345678') > 0
SELECT TOP (1) *
FROM [InformHelper].[dbo].[InformMasterNamesTST]
WHERE [LastName] = 'SMITH'
AND [FirstName] = 'JOHN'
AND DOB = '01/01/1990'
AND DL = 'TX12345678') > 0
ORDER BY [NamelastUpdated] DESC, [NameEnterDate] DESC
ELSE IF (SELECT TOP (1) COUNT(*)
FROM [InformHelper].[dbo].[InformMasterNamesTST]
WHERE [LastName] = 'SMITH'
AND [FirstName] = 'JOHN'
AND DOB = '01/01/1990'
AND SSN = '12345678901') > 0
SELECT TOP (1) *
FROM [InformHelper].[dbo].[InformMasterNamesTST]
WHERE [LastName] = 'SMITH'
AND [FirstName] = 'JOHN'
AND DOB = '01/01/1990'
AND SSN = '12345678901') > 0
ORDER BY [NamelastUpdated] DESC, [NameEnterDate] DESC
ELSE
SELECT 0
Which would return the below from my second table:
Name | LastName | FirstName | MiddleName | SEX | RACE | DOB | DL | SSN
10173538 | SMITH | JOHN | SHAWN | M | W | 01/01/1990 | | 1234567890
Basically I would like it to do a "for each" for each name in the first table to search the IF statement I created. I'm getting confused on how to query each name in the IF statement. Since some names do not have a DL, SSN, Middle name, DOB, etc if have to make sure it checks for possible matches.
If it finds a match I want it to "do something". Which I want it to be able to reference the photo path column from the first table. So I somehow need to pass that to the "do something" part.