I have three tables that I want to join together and create a view as a lookup. The problem is is that I am getting duplicate provider id
because the provider_name
is different.
Table1
provider_id provider_name
545 Kent Hospital
Table2
provider_id provider_name
565 Devin Hospital
table 3
provider_id provider_name
545 Kent Medical Center
I am getting :
provider_id provider_name
545 Kent Hospital
545 Kent Medical Center
565 Devin Hospital
final expected result:
provider_id provider_name
545 Kent Hospital
565 Devin Hospital
Realistically, it doesn't matter which name is used since they are usually almost the same.
But the problem is that I am getting duplicates in provider_id
and that's leading into duplicate records on my joins to other tables. I know there are ways to avoid this on the joins to the end-point tables using approaches like this, but to me this seems like a bandage rather than a solution at the core.
my current approach:
--CREATE VIEW lookup_providers AS
SELECT DISTINCT provider_id, provider_name
FROM table1
UNION
SELECT DISTINCT provider_id, provider_name
FROM table2
UNION
SELECT DISTINCT provider_id, provider_name
FROM table3
Sample tables
CREATE TABLE table1 (provider_id nvarchar(30), provider_name nvarchar(30))
CREATE TABLE table2 (provider_id nvarchar(30), provider_name nvarchar(30))
CREATE TABLE table3 (provider_id nvarchar(30), provider_name nvarchar(30))
INSERT INTO table1 (provider_id, provider_name) VALUES ('545','Kent Hospital')
INSERT INTO table2 (provider_id, provider_name) VALUES ('565','Devin Hospital')
INSERT INTO table3 (provider_id, provider_name) VALUES ('545','Kent Medical Center')