我有一個包含3個字段的數據列表,ISIN,CompanyId和EffectiveDate。 ISIN在公司ID和生效日期都被複制。字段1在字段2上覆制,需要根據字段3選擇1,並返回字段1和字段2
實施例的數據:
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
使用
SELECT ISIN, CompanyId, MAX(EffectiveDate)
FROM [MappingData].[dbo].[ESGMappingISIN]
GROUP BY ISIN,CompanyId
我可以刪除關於ISIN和CompanyId重複項以返回
ISIN CompanyId EffectiveDate
AED001410045 58105545 2012-02-09
AEDFXA1G39P8 132844116 2017-09-21
ARDEUT110020 249603 2012-02-09
ARDEUT110020 416264458 2017-03-22
的最後一步,我需要的是:
A) Return ISIN and CompanyId where ISIN is distinct
B) If 1 ISIN has 2 CompanyIds, return the line with the later effective date.
理想輸出
ISIN CompanyId EffectiveDate
AED001410045 58105545 2012-02-09
AEDFXA1G39P8 132844116 2017-09-21
ARDEUT110020 416264458 2017-03-22
什麼[標籤:RDBMS]您使用的? – Mureinik
使用子查詢獲取所需的行,連接回主表以從中獲取數據列。同樣的邏輯在這裏:https://stackoverflow.com/questions/19432913/select-info-from-table-where-row-has-max-date/19433107#19433107 – Twelfth