I have this query:
SELECT
c.ID, c.Firstname, c.lastname, c.BDaY, c.gender,
cp.code, cp.Citizenship, r.race, e.ethnicity
FROM
Client AS C (nolock)
JOIN
Citizenship AS cp (nolock) ON c.ID = cp.client_ID
JOIN
Race AS r (nolock) ON c.ID = R.Client_ID
JOIN
Ethnicity AS E (nolock) ON E.Client_ID = c.ID
This query will return some of the client's names duplicated because they have different race and ethnicity.
Example:
ID |FirstName|Lastname| BDay | gender | code |citizenship| race | ethnicity
1 Pedram Salamati 01-20-1998 M 1 US citizen Middle-east Spanish
1 Pedram Salamati 01-20-1998 M 1 US Citizen Middle-east unknown
1 Pedram Salamati 01-20-1998 M 1 US Citizen Middle-east Brazilian
2 Jesse Albert 03-05-1982 F 1 US Citizen African not Spanish
2 Jesse Albert 03-05-1982 F 1 US Citizen American not Spanish
I was wondering if there is any way to say if race is not = than Race should be Multiracial and if ethnicity Is not = to each other for same Id choose the last updated one.
PS.Ethnicity
has time stamp and I can use Max(e.LastUpdate)
I think maybe a sub query can help!
Any help or thought will be much appreciated!
Here is some test data to mimic your environment in the future you should separate the tables involved and test data. including DML statements is appropriate and helpful as well so people can try their solution prior to answering.
With those variables you can do the following, there of course is more than 1 way this is simply 1 way I am choosing for a few reasons:
You displayed 2 issue 1 with race and 1 with ethnicity
For Ethnicity: you want to use aggregation to determine which ethnicity to assign. this can also be done with a window function but the way I wrote it here it will account for duplicates to exist even in the Ethnicity table.
For Race: you simply want the latest row partitioned by client you can use the ROW_NUMBER() function to generate that and then select where it equals 1 in the join statement
A third issue that you didn't point out but could be possible in some countries anyway is DUAL CITIZENSHIP. In that case you could use a method similar to that of Race.
Note even though Common Table Expressions [CTE] are used you can actually nest those as subselect as well.