Я протестировал скрипт SQL ниже с некоторыми фиктивными данными, которые я создал, и это сработало. Я также создал представление. Я использовал UNION ALL для извлечения всех данных, а затем использовал оконную функцию для фильтрации дубликатов во внешнем WHERE. Я разделил по LicenseNumber, так как он уникален для животного.
Это не самый красивый, но он получит необходимые данные, пока вы не найдете более чистое решение.
SELECT LicenseYear
, [License Number] AS [LicenseNumber], [Last Name] AS [LastName], [First Name] AS
[Firstname], [Street Name] AS [StreetName], [Address2], [City], [State], [Zip],
[Phone], '' AS [Email], [Reg Number] AS [RegNumber], [Dog Name] AS [DogName], [Dog
Breed] AS [Breed], [Purchased] AS [Year], [Sex], [Altered], [Color], [Vet] AS
[Veterinarian], [RabieTag] AS [RabiesTagNumber], [RabiesTag_Exp] AS
[RabiesTagExpiration], [Dog Number] AS [OwnerID], [Reg Number] AS [TagNumber]
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY LicenseNumber ORDER BY LicenseNumber, LicenseYear DESC ) AS 'RowNum'
, *
FROM (
--SELECT 2020 AS 'LicenseYear'
--, [License Number] AS [LicenseNumber], [Last Name] AS [LastName], [First Name] AS [Firstname], [Street Name] AS [StreetName], [Address2], [City], [State], [Zip], [Phone], '' AS [Email], [Reg Number] AS [RegNumber], [Dog Name] AS [DogName], [Dog Breed] AS [Breed], [Purchased] AS [Year], [Sex], [Altered], [Color], [Vet] AS [Veterinarian], [RabieTag] AS [RabiesTagNumber], [RabiesTag_Exp] AS [RabiesTagExpiration], [Dog Number] AS [OwnerID], [Reg Number] AS [TagNumber]
--FROM [dbo].[Dog_Info_2020]
--WHERE Flag is not null
--UNION ALL
SELECT 2019 AS 'LicenseYear'
, [License Number] AS [LicenseNumber], [Last Name] AS [LastName], [First Name] AS [Firstname], [Street Name] AS [StreetName], [Address2], [City], [State], [Zip], [Phone], '' AS [Email], [Reg Number] AS [RegNumber], [Dog Name] AS [DogName], [Dog Breed] AS [Breed], [Purchased] AS [Year], [Sex], [Altered], [Color], [Vet] AS [Veterinarian], [RabieTag] AS [RabiesTagNumber], [RabiesTag_Exp] AS [RabiesTagExpiration], [Dog Number] AS [OwnerID], [Reg Number] AS [TagNumber]
FROM [dbo].[Dog_Info_2019]
WHERE Flag is not null
UNION ALL
SELECT 2018 AS 'LicenseYear'
, [License Number] AS [LicenseNumber], [Last Name] AS [LastName], [First Name] AS [Firstname], [Street Name] AS [StreetName], [Address2], [City], [State], [Zip], [Phone], '' AS [Email], [Reg Number] AS [RegNumber], [Dog Name] AS [DogName], [Dog Breed] AS [Breed], [Purchased] AS [Year], [Sex], [Altered], [Color], [Vet] AS [Veterinarian], [RabieTag] AS [RabiesTagNumber], [RabiesTag_Exp] AS [RabiesTagExpiration], [Dog Number] AS [OwnerID], [Reg Number] AS [TagNumber]
FROM [dbo].[Dog_Info_2018]
WHERE Flag is not null
) Data
) Data
WHERE RowNum = 1