I have a list of data with 3 fields, ISIN, CompanyId, and EffectiveDate. ISIN gets duplicated on both CompanyId and Effective Date.
Example Data:
ISIN CompanyId EffectiveDate
AED001410045 58105545 2012-02-09
AEDFXA1G39P8 132844116 2016-04-22
AEDFXA1G39P8 132844116 2017-09-21
ARDEUT110020 249603 2012-02-09
ARDEUT110020 416264458 2017-03-22
Using
SELECT ISIN, CompanyId, MAX(EffectiveDate)
FROM [MappingData].[dbo].[ESGMappingISIN]
GROUP BY ISIN,CompanyId
I can remove the duplicates on ISIN and CompanyId to return
ISIN CompanyId EffectiveDate
AED001410045 58105545 2012-02-09
AEDFXA1G39P8 132844116 2017-09-21
ARDEUT110020 249603 2012-02-09
ARDEUT110020 416264458 2017-03-22
The last step that I need is to:
A) Return ISIN and CompanyId where ISIN is distinct
B) If 1 ISIN has 2 CompanyIds, return the line with the later effective date.
Ideal output
ISIN CompanyId EffectiveDate
AED001410045 58105545 2012-02-09
AEDFXA1G39P8 132844116 2017-09-21
ARDEUT110020 416264458 2017-03-22