-
Notifications
You must be signed in to change notification settings - Fork 0
/
hl7_export_records_akron0522_0925.sql
233 lines (215 loc) · 7.28 KB
/
hl7_export_records_akron0522_0925.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
delimiter &
CREATE EVENT hl7_export_records_akron0522_0925
ON SCHEDULE
EVERY 1 day
STARTS '2017-12-21 14:25:00'
COMMENT 'pick up every new records that are more than 10 seconds old'
DO
BEGIN
UPDATE LOW_PRIORITY hl7app.adt_msg_queue
SET processing_status= 'p'
WHERE processing_status = 'r'
AND (customer_id = 'AKRON0522')
AND msg_type = 'A03'
AND system_timestamp < now() - 10;
UPDATE LOW_PRIORITY hl7app.adt_msg_queue
SET processing_status= 'c'
WHERE processing_status = 'r'
AND (customer_id = 'AKRON0522')
AND (msg_type = 'A04' or msg_type = 'A08')
AND visit_number in (
SELECT v_number
FROM (
SELECT distinct visit_number AS v_number
FROM hl7app.adt_msg_queue
WHERE msg_type = 'A03'
AND processing_status= 'p'
) AS c
);
UPDATE LOW_PRIORITY hl7app.adt_msg_queue
SET processing_status= 'p'
WHERE processing_status = 'r'
AND (customer_id = 'AKRON0522')
AND msg_type = 'A04'
AND system_timestamp < now() - INTERVAL 1 DAY;
UPDATE LOW_PRIORITY hl7app.adt_msg_queue amq
INNER JOIN (
select adt.visit_number, MAX(adt.system_timestamp) as maxTS from adt_msg_queue adt
group by adt.visit_number
) ms on amq.visit_number = ms.visit_number AND amq.system_timestamp = maxTS
SET processing_status= 'p'
WHERE amq.processing_status = 'r'
AND (amq.customer_id = 'AKRON0522')
AND amq.msg_type = 'A08'
AND amq.visit_number in (
SELECT v_number
FROM (
SELECT distinct mq.visit_number AS v_number
FROM hl7app.adt_msg_queue mq
WHERE mq.msg_type = 'A04'
AND mq.processing_status= 'p'
AND (mq.customer_id = 'AKRON0522')
GROUP by v_number
) AS c
);
UPDATE LOW_PRIORITY hl7app.adt_msg_queue
SET processing_status= 'c'
WHERE processing_status = 'r'
AND (customer_id = 'AKRON0522')
AND msg_type = 'A08'
AND visit_number in (
SELECT v_number
FROM (
SELECT distinct visit_number as v_number
FROM hl7app.adt_msg_queue
WHERE msg_type = 'A04'
AND processing_status= 'p'
AND (customer_id = 'AKRON0522')
) AS c
);
UPDATE LOW_PRIORITY hl7app.adt_msg_queue
SET processing_status= 'c'
WHERE processing_status = 'p'
AND (customer_id = 'AKRON0522')
AND msg_type = 'A04'
AND visit_number in (
SELECT v_number
FROM (
SELECT distinct visit_number as v_number
FROM hl7app.adt_msg_queue
WHERE msg_type = 'A08'
AND processing_status= 'p'
AND (customer_id = 'AKRON0522')
) AS c
);
SET @sql_text_select =
CONCAT (
"SELECT 'PatientNameGiven',
'PatientNameSecondGiven',
'PatientNameFamily',
'PatientNameSuffix',
'AddressStreet1',
'AddressStreet2',
'AddressCity',
'AddressState',
'AddressPostalCode',
'PhoneAreaCityCode',
'PhoneLocalNumber',
'MRN',
'DateOfBirth',
'AdministrativeSex',
'PrimaryLanguage',
'Race',
'EthnicGroup',
'MaritalStatus',
'Email',
'PatientClass',
'FacilityName',
'FacilityNPI',
'FacilityNumber',
'VisitNumber',
'AdmitDateTime',
'DischargeDateTime',
'AdmitSource',
'DischargeStatus',
'DischargeUnitID',
'DischargeUnitName',
'MSDRG',
'DiagnosisPrimaryICD10',
'Diagnosis2ICD10',
'Diagnosis3ICD10',
'IsDeceased',
'ICU',
'EDAdmit',
'InsuranceCompanyID',
'InsuranceCompanyName',
'ClinicName',
'ClinicNPI',
'ClinicID',
'AttendingDoctorNameGiven',
'AttendingDoctorNameSecondGiven',
'AttendingDoctorNameFamily',
'AttendingDoctorDegree',
'AttendingDoctorNPI',
'AttendingDoctorAdministrativeSex',
'AttendingDoctorSpecialty',
'AttendingDoctorID',
'PCPID',
'ProcedurePrimaryCPT',
'Procedure2CPT',
'Procedure3CPT',
'ServiceIndicator01' "
," UNION ALL "
,"SELECT patient_first_name as 'PatientNameGiven',
patient_middle_name as 'PatientNameSecondGiven',
patient_last_name as 'PatientNameFamily',
patient_suffix as 'PatientNameSuffix',
address1 as 'AddressStreet1',
address2 as 'AddressStreet2',
city as 'AddressCity',
state as 'AddressState',
zip as 'AddressPostalCode',
area_code as 'PhoneAreaCityCode',
local_number as 'PhoneLocalNumber',
mrn as 'MRN',
dob as 'DateOfBirth',
gender as 'AdministrativeSex',
language as 'PrimaryLanguage',
race as 'Race',
ethnic_group as 'EthnicGroup',
marital as 'MaritalStatus',
email_address as 'Email',
visit_type as 'PatientClass',
sending_facility_name as 'FacilityName',
'' as 'FacilityNPI',
sending_facility_id as 'FacilityNumber',
visit_number as 'VisitNumber',
admit_datetime as 'AdmitDateTime',
discharge_datetime as 'DischargeDateTime',
admit_source as 'AdmitSource',
discharge_status as 'DischargeStatus',
location as 'DischargeUnitID',
'' as 'DischargeUnitName',
'' as 'MSDRG',
primary_diagnosis as 'DiagnosisPrimaryICD10',
secondary_diagnosis as 'Diagnosis2ICD10',
tertiary_diagnosis as 'Diagnosis3ICD10',
death_indicator as 'IsDeceased',
'' as 'ICU',
'' as 'EDAdmit',
primary_payer_id as 'InsuranceCompanyID',
primary_payer_name as 'InsuranceCompanyName',
'' as 'ClinicName',
'' as 'ClinicNPI',
'' as 'ClinicID',
attending_doctor_first_name as 'AttendingDoctorNameGiven',
attending_doctor_middle_name as 'AttendingDoctorNameSecondGiven',
attending_doctor_last_name as 'AttendingDoctorNameFamily',
attending_doctor_degree as 'AttendingDoctorDegree',
attending_doctor_id as 'AttendingDoctorNPI',
'' as 'AttendingDoctorAdministrativeSex',
'' as 'AttendingDoctorSpecialty',
'' as 'AttendingDoctorID',
'' as 'PCPID',
'' as 'ProcedurePrimaryCPT',
'' as 'Procedure2CPT',
'' as 'Procedure3CPT',
'' as 'ServiceIndicator01'"
," into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/AKRON0522_HL7_"
, DATE_FORMAT( NOW(), '%Y%m%d%H%i%S%f')
, " ' FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '\"'
ESCAPED BY '\"'
LINES TERMINATED BY '\n'
FROM hl7app.adt_msg_queue
WHERE processing_status = 'p'
AND (customer_id = 'AKRON0522');"
);
PREPARE s1 FROM @sql_text_select;
EXECUTE s1;
DROP PREPARE s1;
UPDATE hl7app.adt_msg_queue
SET processing_status= 'd'
WHERE processing_status = 'p'
AND (customer_id = 'AKRON0522');
END &
delimiter ;