TheBugSlayer
Programmer
Hello everyone.
I have had to split a MERGE statement into two separate ones because I could not figure out why duplicates were added after the second execution. The source of the first MERGE, code commented in my statement, is providers with an NPI number; 2032 in total. The source of the second MERGE below is 2308 providers without an NPI number. So I am expecting to see 4340 providers in the target table. However there are only 4231 providers after both statements are executed.
In the source table (for both MERGE source queries), some providers exist WITH and WITHOUT an NPI number. I run the first MERGE and all is fine. However the second MERGE only inserts apparently less records than the 2308 expected because the total at the end of the execution is 4231 instead of 4340.
I have not been able to figure out how to modify the ON and WHEN MATCHED clauses to get the full complement of providers in the target table. Below is my code:
Any help is greatly appreciated.
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA), Training HOTT cert SQL Server 2012 Business Intelligence (SSIS, SSAS).
I have had to split a MERGE statement into two separate ones because I could not figure out why duplicates were added after the second execution. The source of the first MERGE, code commented in my statement, is providers with an NPI number; 2032 in total. The source of the second MERGE below is 2308 providers without an NPI number. So I am expecting to see 4340 providers in the target table. However there are only 4231 providers after both statements are executed.
In the source table (for both MERGE source queries), some providers exist WITH and WITHOUT an NPI number. I run the first MERGE and all is fine. However the second MERGE only inserts apparently less records than the 2308 expected because the total at the end of the execution is 4231 instead of 4340.
I have not been able to figure out how to modify the ON and WHEN MATCHED clauses to get the full complement of providers in the target table. Below is my code:
Code:
MERGE INTO dbo.StageReferringProvider target
USING (
SELECT temp.LastName, temp.FirstName, temp.MiddleName, temp.Degree, LTRIM(RTRIM(NPI)) AS NPI, temp.Sub, temp.PracticeName, CHECKSUM(temp.PracticeName) AS PracticeHash
FROM (
--Providers WITH an NPI number
--SELECT ROW_NUMBER() OVER (PARTITION BY NPI ORDER BY NPI) AS RowNumber, *
--FROM ufnBernhardt_ReferringProviders()
--WHERE LEN(NPI) > 0
--UNION
SELECT ROW_NUMBER() OVER (PARTITION BY LastName, FirstName ORDER BY LastName, FirstName) AS RowNumber, *
FROM ufnBernhardt_ReferringProviders()
WHERE LEN(NPI) <= 0 AND (LEN(LastName) > 0 AND LEN(FirstName) > 0)
) AS temp
WHERE temp.RowNumber = 1
) AS source
[b][COLOR=red] ON target.LastName = source.LastName AND target.FirstName = source.FirstName
WHEN MATCHED
AND LEN(target.NPI) <= 0 AND (target.MiddleName <> source.MiddleName OR target.Degree <> source.Degree [/color])[/b]
THEN UPDATE SET target.MiddleName = source.MiddleName, target.Degree = source.Degree
WHEN NOT MATCHED THEN INSERT VALUES(source.LastName, source.FirstName, source.MiddleName, source.Degree, LTRIM(RTRIM(source.NPI)), GETDATE(), GETDATE())
OUTPUT COALESCE(Inserted.RID, Deleted.RID), $action, source.PracticeHash, source.PracticeName, source.Sub INTO #MergeReferringProviderOutput;
Any help is greatly appreciated.
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA), Training HOTT cert SQL Server 2012 Business Intelligence (SSIS, SSAS).