-
Notifications
You must be signed in to change notification settings - Fork 7
/
npidata_refactor.sql
204 lines (190 loc) · 6.12 KB
/
npidata_refactor.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
-- Load the npidata table from the staging table
-- Does not include the Taxonomy and Other Provider entities
USE NPPES
Insert into dbo.npidata (
NPI,
Entity_Type_Code,
Replacement_NPI,
Employer_Identification_Number,
Provider_Org_Name,
Provider_Last_Name,
Provider_First_Name,
Provider_Middle_Name,
Provider_Name_Prefix_Text,
Provider_Name_Suffix_Text,
Provider_Credential_Text,
Provider_Other_Organization_Name,
Provider_Other_Organization_Name_Type_Code,
Provider_Other_Last_Name,
Provider_Other_First_Name,
Provider_Other_Middle_Name,
Provider_Other_Name_Prefix_Text,
Provider_Other_Name_Suffix_Text,
Provider_Other_Credential_Text,
Provider_Other_Last_Name_Type_Code,
Provider_First_Line_Business_Mailing_Address,
Provider_Second_Line_Business_Mailing_Address,
Provider_Business_Mailing_Address_City_Name,
Provider_Business_Mailing_Address_State_Name,
Provider_Business_Mailing_Address_Postal_Code,
Provider_Business_Mailing_Address_Country_Code,
Provider_Business_Mailing_Address_Telephone_Number,
Provider_Business_Mailing_Address_Fax_Number,
Provider_First_Line_Business_Practice_Location_Address,
Provider_Second_Line_Business_Practice_Location_Address,
Provider_Business_Practice_Location_Address_City_Name,
Provider_Business_Practice_Location_Address_State_Name,
Provider_Business_Practice_Location_Address_Postal_Code,
Provider_Business_Practice_Location_Address_Country_Code,
Provider_Business_Practice_Location_Address_Telephone_Number,
Provider_Business_Practice_Location_Address_Fax_Number,
Provider_Enumeration_Date,
Last_Update_Date,
NPI_Deactivation_Reason_Code,
NPI_Deactivation_Date,
NPI_Reactivation_Date,
Provider_Gender_Code,
Authorized_Official_Last_Name,
Authorized_Official_First_Name,
Authorized_Official_Middle_Name,
Authorized_Official_Title_or_Position,
Authorized_Official_Telephone_Number,
Is_Sole_Proprietor,
Is_Organization_Subpart,
Parent_Organization_LBN,
Parent_Organization_TIN,
Authorized_Official_Name_Prefix_Text,
Authorized_Official_Name_Suffix_Text,
Authorized_Official_Credential_Text,
Certification_Date
)
select
NPI,
Entity_Type_Code,
Replacement_NPI,
Employer_Identification_Number,
Provider_Org_Name,
Provider_Last_Name,
Provider_First_Name,
Provider_Middle_Name,
Provider_Name_Prefix_Text,
Provider_Name_Suffix_Text,
Provider_Credential_Text,
Provider_Other_Organization_Name,
Provider_Other_Organization_Name_Type_Code,
Provider_Other_Last_Name,
Provider_Other_First_Name,
Provider_Other_Middle_Name,
Provider_Other_Name_Prefix_Text,
Provider_Other_Name_Suffix_Text,
Provider_Other_Credential_Text,
Provider_Other_Last_Name_Type_Code,
Provider_First_Line_Business_Mailing_Address,
Provider_Second_Line_Business_Mailing_Address,
Provider_Business_Mailing_Address_City_Name,
Provider_Business_Mailing_Address_State_Name,
Provider_Business_Mailing_Address_Postal_Code,
Provider_Business_Mailing_Address_Country_Code,
Provider_Business_Mailing_Address_Telephone_Number,
Provider_Business_Mailing_Address_Fax_Number,
Provider_First_Line_Business_Practice_Location_Address,
Provider_Second_Line_Business_Practice_Location_Address,
Provider_Business_Practice_Location_Address_City_Name,
Provider_Business_Practice_Location_Address_State_Name,
Provider_Business_Practice_Location_Address_Postal_Code,
Provider_Business_Practice_Location_Address_Country_Code,
Provider_Business_Practice_Location_Address_Telephone_Number,
Provider_Business_Practice_Location_Address_Fax_Number,
cast(Provider_Enumeration_Date as date),
cast(Last_Update_Date as date),
NPI_Deactivation_Reason_Code,
cast(NPI_Deactivation_Date as date),
cast(NPI_Reactivation_Date as date),
Provider_Gender_Code,
Authorized_Official_Last_Name,
Authorized_Official_First_Name,
Authorized_Official_Middle_Name,
Authorized_Official_Title_or_Position,
Authorized_Official_Telephone_Number,
Is_Sole_Proprietor,
Is_Organization_Subpart,
Parent_Organization_LBN,
Parent_Organization_TIN,
Authorized_Official_Name_Prefix_Text,
Authorized_Official_Name_Suffix_Text,
Authorized_Official_Credential_Text,
cast(Certification_Date as date)
from dbo.npidata_stage;
GO
-- Denormalize the taxonomy (aka provider specialty) fields into a separate table
-- This approach uses dynamic SQL to minimize redundancy in the code
DECLARE @template nvarchar(max);
DECLARE @sql nvarchar(max);
SET @template = N'
insert into dbo.npi_taxonomy (
NPI,
Healthcare_Provider_Taxonomy_Code,
Provider_License_Number,
Provider_License_Number_State_Code,
Healthcare_Provider_Primary_Taxonomy_Switch,
Healthcare_Provider_Taxonomy_Group
)
select
NPI,
Healthcare_Provider_Taxonomy_Code_@anchor,
Provider_License_Number_@anchor,
Provider_License_Number_State_Code_@anchor,
Healthcare_Provider_Primary_Taxonomy_Switch_@anchor,
Healthcare_Provider_Taxonomy_Group_@anchor
from dbo.npidata_stage stage
where stage.Healthcare_Provider_Taxonomy_Code_@anchor IS NOT NULL;'
DECLARE @offset nvarchar(4);
DECLARE @counter INT = 1;
WHILE @counter <= 15
BEGIN
SET @offset = ltrim(str(@counter));
SET @sql = @template;
SET @sql = REPLACE(@sql, '@anchor', @offset);
EXEC sp_executesql @sql;
SET @counter = @counter + 1;
END
GO
--
-- Denormalize the Other Provider information
--
--
USE NPPES
DECLARE @template2 nvarchar(max);
DECLARE @sql2 nvarchar(max);
SET @template2 = N'
insert into dbo.npi_oth_prov (
NPI,
Other_Provider_Identifier,
Other_Provider_Identifier_Type_Code,
Other_Provider_Identifier_State,
Other_Provider_Identifier_Issuer
)
select
NPI,
Other_Provider_Identifier_@anchor,
Other_Provider_Identifier_Type_Code_@anchor,
Other_Provider_Identifier_State_@anchor,
Other_Provider_Identifier_Issuer_@anchor
from dbo.npidata_stage stage
where stage.Other_Provider_Identifier_@anchor IS NOT NULL;'
DECLARE @offset2 nvarchar(4);
DECLARE @counter2 INT = 1;
WHILE @counter2 <= 50
BEGIN
SET @offset2 = ltrim(str(@counter2));
SET @sql2 = @template2;
SET @sql2 = REPLACE(@sql2, '@anchor', @offset2);
EXEC sp_executesql @sql2;
SET @counter2 = @counter2 + 1;
END
GO
-- Update Statistics on the tables loaded
USE NPPES
UPDATE STATISTICS dbo.npidata;
UPDATE STATISTICS dbo.npi_taxonomy;
UPDATE STATISTICS dbo.npi_oth_prov;