malawi_hiv_program_reports 1.0.17 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -99,37 +99,37 @@ module MalawiHivProgramReports
99
99
  person.birthdate,
100
100
  disaggregated_age_group(person.birthdate, DATE(#{end_date})) AS age_group,
101
101
  GROUP_CONCAT(DISTINCT orders.concept_id SEPARATOR ',') AS drug_concepts
102
- FROM person
103
- LEFT JOIN patient_identifier
102
+ FROM person #{current_partition}
103
+ LEFT JOIN patient_identifier #{current_partition}
104
104
  ON patient_identifier.patient_id = person.person_id
105
105
  AND patient_identifier.voided = 0
106
- AND patient_identifier.identifier_type IN (SELECT patient_identifier_type_id FROM patient_identifier_type WHERE name = 'ARV Number')
107
- INNER JOIN patient_program
106
+ AND patient_identifier.identifier_type IN (SELECT patient_identifier_type_id FROM patient_identifier_type #{current_partition} WHERE name = 'ARV Number')
107
+ INNER JOIN patient_program #{current_partition}
108
108
  ON patient_program.patient_id = person.person_id
109
- AND patient_program.program_id IN (SELECT program_id FROM program WHERE name = 'HIV PROGRAM')
109
+ AND patient_program.program_id IN (SELECT program_id FROM program #{current_partition} WHERE name = 'HIV PROGRAM')
110
110
  AND patient_program.voided = 0
111
- INNER JOIN patient_state
111
+ INNER JOIN patient_state #{current_partition}
112
112
  ON patient_state.patient_program_id = patient_program.patient_program_id
113
113
  AND patient_state.state = 7 /* State: 7 == On antiretrovirals */
114
114
  AND patient_state.start_date < DATE(#{start_date})
115
115
  AND patient_state.voided = 0
116
- INNER JOIN encounter AS prescription_encounter
116
+ INNER JOIN encounter #{current_partition} AS prescription_encounter
117
117
  ON prescription_encounter.patient_id = patient_program.patient_id
118
- AND prescription_encounter.program_id IN (SELECT program_id FROM program WHERE name = 'HIV PROGRAM')
118
+ AND prescription_encounter.program_id IN (SELECT program_id FROM program #{current_partition} WHERE name = 'HIV PROGRAM')
119
119
  AND prescription_encounter.encounter_type IN (SELECT encounter_type_id FROM encounter_type WHERE name = 'Treatment')
120
120
  AND prescription_encounter.encounter_datetime >= DATE(#{start_date}) - INTERVAL 6 MONTH
121
121
  AND prescription_encounter.encounter_datetime < DATE(#{start_date})
122
122
  AND prescription_encounter.voided = 0
123
- INNER JOIN orders
123
+ INNER JOIN orders #{current_partition}
124
124
  ON orders.encounter_id = prescription_encounter.encounter_id
125
- AND orders.order_type_id IN (SELECT order_type_id FROM order_type WHERE name = 'Drug order')
125
+ AND orders.order_type_id IN (SELECT order_type_id FROM order_type #{current_partition} WHERE name = 'Drug order')
126
126
  AND orders.start_date >= DATE(#{start_date}) - INTERVAL 6 MONTH
127
127
  AND orders.start_date < DATE(#{start_date})
128
128
  AND orders.voided = 0
129
- INNER JOIN concept_name
129
+ INNER JOIN concept_name #{current_partition}
130
130
  ON concept_name.concept_id = orders.concept_id
131
131
  AND concept_name.name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')
132
- INNER JOIN drug_order
132
+ INNER JOIN drug_order #{current_partition}
133
133
  ON drug_order.order_id = orders.order_id
134
134
  AND drug_order.quantity > 0
135
135
  WHERE person.voided = 0
@@ -139,39 +139,39 @@ module MalawiHivProgramReports
139
139
  are classified as new on TPT.
140
140
  */
141
141
  SELECT DISTINCT encounter.patient_id
142
- FROM encounter
143
- INNER JOIN orders
142
+ FROM encounter #{current_partition}
143
+ INNER JOIN orders #{current_partition}
144
144
  ON orders.encounter_id = encounter.encounter_id
145
- AND orders.concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Rifapentine', 'Isoniazid') AND voided = 0)
146
- AND orders.order_type_id IN (SELECT order_type_id FROM order_type WHERE name = 'Drug order')
145
+ AND orders.concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name IN ('Rifapentine', 'Isoniazid') AND voided = 0)
146
+ AND orders.order_type_id IN (SELECT order_type_id FROM order_type #{current_partition} WHERE name = 'Drug order')
147
147
  AND orders.start_date < DATE(#{start_date}) - INTERVAL 6 MONTH
148
148
  AND orders.start_date >= DATE(#{start_date}) - INTERVAL 15 MONTH
149
149
  AND orders.voided = 0
150
- INNER JOIN drug_order
150
+ INNER JOIN drug_order #{current_partition}
151
151
  ON drug_order.order_id = orders.order_id
152
152
  AND drug_order.quantity > 0
153
- WHERE encounter.program_id IN (SELECT program_id FROM program WHERE name = 'HIV PROGRAM')
154
- AND encounter.encounter_type IN (SELECT encounter_type_id FROM encounter_type WHERE name = 'Treatment')
153
+ WHERE encounter.program_id IN (SELECT program_id FROM program #{current_partition} WHERE name = 'HIV PROGRAM')
154
+ AND encounter.encounter_type IN (SELECT encounter_type_id FROM WHERE name = 'Treatment')
155
155
  AND encounter.encounter_datetime < DATE(#{start_date}) - INTERVAL 6 MONTH
156
156
  AND encounter.encounter_datetime >= DATE(#{start_date}) - INTERVAL 15 MONTH
157
157
  AND encounter.voided = 0
158
158
  ) AND person.person_id NOT IN (
159
159
  /* External consultations */
160
160
  SELECT DISTINCT registration_encounter.patient_id
161
- FROM patient_program
162
- INNER JOIN program ON program.name = 'HIV PROGRAM'
163
- INNER JOIN encounter AS registration_encounter
161
+ FROM patient_program #{current_partition}
162
+ INNER JOIN program #{current_partition} ON program.name = 'HIV PROGRAM'
163
+ INNER JOIN encounter #{current_partition} AS registration_encounter
164
164
  ON registration_encounter.patient_id = patient_program.patient_id
165
165
  AND registration_encounter.program_id = patient_program.program_id
166
166
  AND registration_encounter.encounter_datetime < DATE(#{end_date}) + INTERVAL 1 DAY
167
167
  AND registration_encounter.voided = 0
168
168
  INNER JOIN (
169
169
  SELECT MAX(encounter.encounter_datetime) AS encounter_datetime, encounter.patient_id
170
- FROM encounter
170
+ FROM encounter #{current_partition}
171
171
  INNER JOIN encounter_type
172
172
  ON encounter_type.encounter_type_id = encounter.encounter_type
173
173
  AND encounter_type.name = 'Registration'
174
- INNER JOIN program
174
+ INNER JOIN program #{current_partition}
175
175
  ON program.program_id = encounter.program_id
176
176
  AND program.name = 'HIV PROGRAM'
177
177
  WHERE encounter.encounter_datetime < DATE(#{end_date}) AND encounter.voided = 0
@@ -179,10 +179,10 @@ module MalawiHivProgramReports
179
179
  ) AS max_registration_encounter
180
180
  ON max_registration_encounter.patient_id = registration_encounter.patient_id
181
181
  AND max_registration_encounter.encounter_datetime = registration_encounter.encounter_datetime
182
- INNER JOIN obs AS patient_type_obs
182
+ INNER JOIN obs #{current_partition} AS patient_type_obs
183
183
  ON patient_type_obs.encounter_id = registration_encounter.encounter_id
184
- AND patient_type_obs.concept_id IN (SELECT concept_id FROM concept_name WHERE name = 'Type of patient' AND voided = 0)
185
- AND patient_type_obs.value_coded IN (SELECT concept_id FROM concept_name WHERE name IN ('Drug refill', 'External consultation') AND voided = 0)
184
+ AND patient_type_obs.concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name = 'Type of patient' AND voided = 0)
185
+ AND patient_type_obs.value_coded IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name IN ('Drug refill', 'External consultation') AND voided = 0)
186
186
  AND patient_type_obs.voided = 0
187
187
  WHERE patient_program.voided = 0
188
188
  )
@@ -129,29 +129,29 @@ module MalawiHivProgramReports
129
129
  person.gender,
130
130
  person.birthdate,
131
131
  #{function_manager(function: 'disaggregated_age_group', location: @location, args: "person.birthdate::date, #{@end_date}::date")} AS age_group
132
- FROM person
133
- LEFT JOIN patient_identifier
132
+ FROM person #{current_partition}
133
+ LEFT JOIN patient_identifier #{current_partition}
134
134
  ON patient_identifier.patient_id = person.person_id
135
135
  AND patient_identifier.voided = 0
136
- AND #{in_manager(column: 'patient_identifier.identifier_type', values: "(SELECT patient_identifier_type_id FROM patient_identifier_type WHERE name = 'ARV Number')")}
136
+ AND #{in_manager(column: 'patient_identifier.identifier_type', values: "(SELECT patient_identifier_type_id FROM patient_identifier_type #{current_partition} WHERE name = 'ARV Number')")}
137
137
  #{site_manager(operator: 'AND', column: 'patient_identifier.site_id', location: @location)}
138
138
  LEFT JOIN (#{current_occupation_query}) AS current_occupation ON current_occupation.person_id = person.person_id
139
139
  #{site_manager(operator: 'AND', column: 'current_occupation.site_id', location: @location)}
140
140
  INNER JOIN(
141
141
  SELECT denominator_encounter.patient_id AS patient_id, patient_state.start_date AS start_date
142
- FROM person
143
- INNER JOIN patient_program
142
+ FROM person #{current_partition}
143
+ INNER JOIN patient_program #{current_partition}
144
144
  ON patient_program.patient_id = person.person_id
145
- AND #{in_manager(column: 'patient_program.program_id', values: "(SELECT program_id FROM program WHERE name = 'HIV PROGRAM')")}
145
+ AND #{in_manager(column: 'patient_program.program_id', values: "(SELECT program_id FROM program #{current_partition} WHERE name = 'HIV PROGRAM')")}
146
146
  AND patient_program.voided = 0
147
- INNER JOIN patient_state
147
+ INNER JOIN patient_state #{current_partition}
148
148
  ON patient_state.patient_program_id = patient_program.patient_program_id
149
149
  AND patient_state.state = 7 /* State: 7 == On antiretrovirals */
150
150
  AND patient_state.start_date < DATE(#{start_date})
151
151
  AND patient_state.voided = 0
152
- INNER JOIN encounter AS denominator_encounter
152
+ INNER JOIN encounter #{current_partition} AS denominator_encounter
153
153
  ON denominator_encounter.patient_id = patient_program.patient_id
154
- AND #{in_manager(column: 'denominator_encounter.program_id', values: "(SELECT program_id FROM program WHERE name = 'HIV PROGRAM')")}
154
+ AND #{in_manager(column: 'denominator_encounter.program_id', values: "(SELECT program_id FROM program #{current_partition} WHERE name = 'HIV PROGRAM')")}
155
155
  AND #{in_manager(column: 'denominator_encounter.encounter_type', values: "(SELECT encounter_type_id FROM encounter_type WHERE name = 'Treatment')")}
156
156
  AND denominator_encounter.encounter_datetime >= #{interval_manager(date: start_date, value: 6, interval: 'MONTH', operator: '-')}
157
157
  AND denominator_encounter.encounter_datetime <= DATE(#{start_date})
@@ -159,23 +159,23 @@ module MalawiHivProgramReports
159
159
  #{site_manager(operator: 'AND', column: 'patient_program.site_id', location: @location)}
160
160
  GROUP BY denominator_encounter.patient_id, patient_state.start_date
161
161
  ) AS denominator_patient ON denominator_patient.patient_id = person.person_id
162
- INNER JOIN encounter AS prescription_encounter
162
+ INNER JOIN encounter #{current_partition} AS prescription_encounter
163
163
  ON prescription_encounter.patient_id = denominator_patient.patient_id
164
164
  AND prescription_encounter.program_id = 1
165
165
  AND #{in_manager(column: 'prescription_encounter.encounter_type', values: "(SELECT encounter_type_id FROM encounter_type WHERE name = 'Treatment')")}
166
166
  AND prescription_encounter.encounter_datetime >= #{interval_manager(date: start_date, value: 6, interval: 'MONTH', operator: '-')}
167
167
  AND prescription_encounter.encounter_datetime <= DATE(#{end_date})
168
168
  AND prescription_encounter.voided = 0
169
- INNER JOIN orders
169
+ INNER JOIN orders #{current_partition}
170
170
  ON orders.encounter_id = prescription_encounter.encounter_id
171
- AND #{in_manager(column: 'orders.order_type_id', values: "(SELECT order_type_id FROM order_type WHERE name = 'Drug order')")}
171
+ AND #{in_manager(column: 'orders.order_type_id', values: "(SELECT order_type_id FROM order_type #{current_partition} WHERE name = 'Drug order')")}
172
172
  AND orders.start_date >= #{interval_manager(date: start_date, value: 6, interval: 'MONTH', operator: '-')}
173
173
  AND orders.start_date <= CAST(#{end_date} as DATE)
174
174
  AND orders.voided = 0
175
- INNER JOIN concept_name
175
+ INNER JOIN concept_name #{current_partition}
176
176
  ON concept_name.concept_id = orders.concept_id
177
177
  AND #{in_manager(column: 'concept_name.name', values: "'Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine'" )}
178
- INNER JOIN drug_order
178
+ INNER JOIN drug_order #{current_partition}
179
179
  ON drug_order.order_id = orders.order_id
180
180
  AND drug_order.quantity > 0
181
181
  WHERE person.voided = 0 #{%w[Military Civilian].include?(@occupation) ? 'AND' : ''} #{occupation_filter(occupation: @occupation, field_name: 'value', table_name: 'current_occupation', include_clause: false)}
@@ -183,20 +183,20 @@ module MalawiHivProgramReports
183
183
  AND person.person_id NOT IN (
184
184
  /* External consultations */
185
185
  SELECT DISTINCT registration_encounter.patient_id
186
- FROM patient_program pp
187
- INNER JOIN program p ON p.program_id = pp.program_id AND p.name = 'HIV PROGRAM' AND p.retired = 0
188
- INNER JOIN encounter AS registration_encounter
186
+ FROM patient_program #{current_partition} pp
187
+ INNER JOIN program #{current_partition} p ON p.program_id = pp.program_id AND p.name = 'HIV PROGRAM' AND p.retired = 0
188
+ INNER JOIN encounter #{current_partition} AS registration_encounter
189
189
  ON registration_encounter.patient_id = pp.patient_id
190
190
  AND registration_encounter.program_id = pp.program_id
191
191
  AND registration_encounter.encounter_datetime < #{interval_manager(date: end_date, value: 1, interval: 'MONTH', operator: '+')}
192
192
  AND registration_encounter.voided = 0
193
193
  INNER JOIN (
194
194
  SELECT MAX(encounter.encounter_datetime) AS encounter_datetime, encounter.patient_id
195
- FROM encounter
195
+ FROM encounter #{current_partition}
196
196
  INNER JOIN encounter_type
197
197
  ON encounter_type.encounter_type_id = encounter.encounter_type
198
198
  AND encounter_type.name = 'Registration'
199
- INNER JOIN program
199
+ INNER JOIN program #{current_partition}
200
200
  ON program.program_id = encounter.program_id
201
201
  AND program.name = 'HIV PROGRAM'
202
202
  WHERE encounter.encounter_datetime < CAST(#{end_date} AS DATE) AND encounter.voided = 0
@@ -204,10 +204,10 @@ module MalawiHivProgramReports
204
204
  ) AS max_registration_encounter
205
205
  ON max_registration_encounter.patient_id = registration_encounter.patient_id
206
206
  AND max_registration_encounter.encounter_datetime = registration_encounter.encounter_datetime
207
- INNER JOIN obs AS patient_type_obs
207
+ INNER JOIN obs #{current_partition} AS patient_type_obs
208
208
  ON patient_type_obs.encounter_id = registration_encounter.encounter_id
209
- AND #{in_manager(column: 'patient_type_obs.concept_id', values: "(SELECT concept_id FROM concept_name WHERE name = 'Type of patient' AND voided = 0)")}
210
- AND #{in_manager(column: 'patient_type_obs.value_coded', values: "(SELECT concept_id FROM concept_name WHERE name IN ('Drug refill', 'External consultation') AND voided = 0)")}
209
+ AND #{in_manager(column: 'patient_type_obs.concept_id', values: "(SELECT concept_id FROM concept_name #{current_partition} WHERE name = 'Type of patient' AND voided = 0)")}
210
+ AND #{in_manager(column: 'patient_type_obs.value_coded', values: "(SELECT concept_id FROM concept_name #{current_partition} WHERE name IN ('Drug refill', 'External consultation') AND voided = 0)")}
211
211
  AND patient_type_obs.voided = 0
212
212
  WHERE pp.voided = 0
213
213
  )
@@ -236,22 +236,22 @@ module MalawiHivProgramReports
236
236
  END AS transfer_in,
237
237
  MAX(o.start_date) AS last_dispensed_date,
238
238
  MAX(o.auto_expire_date) AS auto_expire_date
239
- FROM orders o
240
- INNER JOIN concept_name cn
239
+ FROM orders #{current_partition} o
240
+ INNER JOIN concept_name #{current_partition} cn
241
241
  ON cn.concept_id = o.concept_id
242
242
  AND cn.name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')
243
243
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
244
- LEFT JOIN obs tpt_transfer_in_obs
244
+ LEFT JOIN obs #{current_partition} tpt_transfer_in_obs
245
245
  ON tpt_transfer_in_obs.person_id = o.patient_id
246
246
  AND tpt_transfer_in_obs.concept_id = #{::ConceptName.find_by_name('TPT Drugs Received').concept_id}
247
247
  AND tpt_transfer_in_obs.voided = 0
248
248
  #{site_manager(operator: 'AND', column: 'tpt_transfer_in_obs.site_id', location: @location)}
249
- AND tpt_transfer_in_obs.value_drug IN (SELECT drug_id FROM drug WHERE concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')))
250
- INNER JOIN drug_order dor
249
+ AND tpt_transfer_in_obs.value_drug IN (SELECT drug_id FROM drug #{current_partition} WHERE concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')))
250
+ INNER JOIN drug_order #{current_partition} dor
251
251
  ON dor.order_id = o.order_id
252
252
  AND dor.quantity > 0
253
253
  WHERE DATE(o.start_date) BETWEEN DATE(#{c_start_date}) AND DATE(#{c_end_date})
254
- AND o.order_type_id IN (SELECT order_type_id FROM order_type WHERE name = 'Drug order')
254
+ AND o.order_type_id IN (SELECT order_type_id FROM order_type #{current_partition} WHERE name = 'Drug order')
255
255
  AND o.voided = 0
256
256
  AND o.patient_id = #{patient_id}
257
257
  GROUP BY o.patient_id
@@ -291,11 +291,11 @@ module MalawiHivProgramReports
291
291
  WHEN o.value_drug = #{isoniazid_rifapentine_drug.drug_id} THEN '3HP'
292
292
  ELSE '6H'
293
293
  END AS course
294
- FROM obs o
294
+ FROM obs #{current_partition} o
295
295
  WHERE o.concept_id = #{::ConceptName.find_by_name('TPT Drugs Received').concept_id}
296
296
  AND o.voided = 0
297
297
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
298
- AND o.value_drug IN (SELECT drug_id FROM drug WHERE concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')))
298
+ AND o.value_drug IN (SELECT drug_id FROM drug #{current_partition} WHERE concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')))
299
299
  AND o.person_id = #{patient_id}
300
300
  AND o.value_numeric IS NOT NULL
301
301
  AND DATE(o.obs_datetime) <= DATE(#{start_date})
@@ -312,10 +312,10 @@ module MalawiHivProgramReports
312
312
  WHEN o.concept_id = #{isoniazid_rifapentine_concept.concept_id} THEN '3HP'
313
313
  ELSE '6H'
314
314
  END AS course
315
- FROM orders o
316
- INNER JOIN encounter e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.program_id = 1 /* HIV PROGRAM */
317
- INNER JOIN drug_order dor ON dor.order_id = o.order_id AND dor.quantity > 0
318
- WHERE o.order_type_id IN (SELECT order_type_id FROM order_type WHERE name = 'Drug order')
315
+ FROM orders #{current_partition} o
316
+ INNER JOIN encounter #{current_partition} e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.program_id = 1 /* HIV PROGRAM */
317
+ INNER JOIN drug_order #{current_partition} dor ON dor.order_id = o.order_id AND dor.quantity > 0
318
+ WHERE o.order_type_id IN (SELECT order_type_id FROM order_type #{current_partition} WHERE name = 'Drug order')
319
319
  AND o.voided = 0
320
320
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
321
321
  AND o.concept_id IN (#{::ConceptName.where(name: ['Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine']).select(:concept_id).to_sql})
@@ -340,11 +340,11 @@ module MalawiHivProgramReports
340
340
  WHEN o.value_drug = #{isoniazid_rifapentine_drug.drug_id} THEN '3HP'
341
341
  ELSE '6H'
342
342
  END AS course
343
- FROM obs o
343
+ FROM obs #{current_partition} o
344
344
  WHERE o.concept_id = #{::ConceptName.find_by_name('TPT Drugs Received').concept_id}
345
345
  AND o.voided = 0
346
346
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
347
- AND o.value_drug IN (SELECT drug_id FROM drug WHERE concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')))
347
+ AND o.value_drug IN (SELECT drug_id FROM drug #{current_partition} WHERE concept_id IN (SELECT concept_id FROM concept_name #{current_partition} WHERE name IN ('Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine')))
348
348
  AND o.person_id = #{patient_id}
349
349
  AND o.value_numeric IS NOT NULL
350
350
  AND DATE(o.obs_datetime) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
@@ -361,10 +361,10 @@ module MalawiHivProgramReports
361
361
  WHEN o.concept_id = #{isoniazid_rifapentine_concept.concept_id} THEN '3HP'
362
362
  ELSE '6H'
363
363
  END AS course
364
- FROM orders o
365
- INNER JOIN encounter e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.program_id = 1 /* HIV PROGRAM */
366
- INNER JOIN drug_order dor ON dor.order_id = o.order_id AND dor.quantity > 0
367
- WHERE o.order_type_id IN (SELECT order_type_id FROM order_type WHERE name = 'Drug order')
364
+ FROM orders #{current_partition} o
365
+ INNER JOIN encounter #{current_partition} e ON e.encounter_id = o.encounter_id AND e.voided = 0 AND e.program_id = 1 /* HIV PROGRAM */
366
+ INNER JOIN drug_order #{current_partition} dor ON dor.order_id = o.order_id AND dor.quantity > 0
367
+ WHERE o.order_type_id IN (SELECT order_type_id FROM order_type #{current_partition} WHERE name = 'Drug order')
368
368
  AND o.voided = 0
369
369
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
370
370
  AND o.concept_id IN (#{::ConceptName.where(name: ['Rifapentine', 'Isoniazid', 'Isoniazid/Rifapentine']).select(:concept_id).to_sql})
@@ -60,7 +60,7 @@ module MalawiHivProgramReports
60
60
  p.person_id patient_id, birthdate, gender,
61
61
  #{function_manager(function: 'pepfar_patient_outcome', location: @location, args: "p.person_id::int, '#{end_date.to_date}'::date, #{@location}::int")} outcome,
62
62
  #{function_manager(function: 'disaggregated_age_group', location: @location, args: "p.birthdate::date, '#{end_date.to_date}::date'")} age_group
63
- FROM person p
63
+ FROM person #{current_partition} p
64
64
  WHERE #{in_manager(column: 'p.person_id', values: patient_ids)}
65
65
  #{site_manager(operator: 'AND', column: 'p.site_id', location: @location)}
66
66
  GROUP BY p.person_id #{group_by_columns('p.birthdate, p.gender')}
@@ -117,10 +117,10 @@ module MalawiHivProgramReports
117
117
  pe.gender,
118
118
  #{function_manager(function: 'patient_date_enrolled', location: @location, args: "p.patient_id::int, #{@location}::int")} AS date_enrolled,
119
119
  #{function_manager(function: 'date_antiretrovirals_started', location: @location, args: "p.patient_id::int, MIN(s.start_date)::date, #{@location}::int")} AS earliest_start_date
120
- FROM patient_program p
121
- INNER JOIN person pe ON pe.person_id = p.patient_id AND pe.voided = 0
120
+ FROM patient_program #{current_partition} p
121
+ INNER JOIN person #{current_partition} pe ON pe.person_id = p.patient_id AND pe.voided = 0
122
122
  #{site_manager(operator: 'AND', column: 'pe.site_id', location: @location)}
123
- INNER JOIN patient_state s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
123
+ INNER JOIN patient_state #{current_partition} s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
124
124
  #{site_manager(operator: 'AND', column: 's.site_id', location: @location)}
125
125
  LEFT JOIN (#{current_occupation_query}) a ON a.person_id = p.patient_id
126
126
  #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
@@ -142,10 +142,10 @@ module MalawiHivProgramReports
142
142
  pe.gender,
143
143
  #{function_manager(function: 'patient_date_enrolled', location: @location, args: "p.patient_id::int, #{@location}::int")} AS date_enrolled,
144
144
  #{function_manager(function: 'date_antiretrovirals_started', location: @location, args: "p.patient_id::int, MIN(s.start_date)::date, #{@location}::int")} AS earliest_start_date
145
- FROM patient_program p
146
- INNER JOIN person pe ON pe.person_id = p.patient_id AND pe.voided = 0
145
+ FROM patient_program #{current_partition} p
146
+ INNER JOIN person #{current_partition} pe ON pe.person_id = p.patient_id AND pe.voided = 0
147
147
  #{site_manager(operator: 'AND', column: 'pe.site_id', location: @location)}
148
- INNER JOIN patient_state s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
148
+ INNER JOIN patient_state #{current_partition} s ON p.patient_program_id = s.patient_program_id AND s.voided = 0 AND s.state = 7
149
149
  #{site_manager(operator: 'AND', column: 's.site_id', location: @location)}
150
150
  LEFT JOIN (#{current_occupation_query}) a ON a.person_id = p.patient_id
151
151
  #{site_manager(operator: 'AND', column: 'a.site_id', location: @location)}
@@ -164,32 +164,32 @@ module MalawiHivProgramReports
164
164
  pp.earliest_start_date,
165
165
  preg_or_breast.name AS maternal_status,
166
166
  DATE(MIN(pregnant_or_breastfeeding.obs_datetime)) AS maternal_status_date
167
- FROM temp_earliest_start_date pp
168
- INNER JOIN person pe ON pe.person_id = pp.patient_id AND pe.voided = 0
167
+ FROM cdr_temp_cohort_members pp
168
+ INNER JOIN person #{current_partition} pe ON pe.person_id = pp.patient_id AND pe.voided = 0
169
169
  #{site_manager(operator: 'AND', column: 'pe.site_id', location: @location)}
170
170
  LEFT JOIN (
171
171
  SELECT max(o.obs_datetime) AS obs_datetime, o.person_id
172
- FROM obs o
172
+ FROM obs #{current_partition} o
173
173
  INNER JOIN concept_name cn ON cn.concept_id = o.concept_id AND cn.name = 'CD4 count' AND cn.voided = 0
174
- INNER JOIN patient_program pp ON pp.patient_id = o.person_id
174
+ INNER JOIN patient_program #{current_partition} pp ON pp.patient_id = o.person_id
175
175
  AND pp.program_id = #{program('HIV PROGRAM').id}
176
176
  AND pp.voided = 0
177
- INNER JOIN patient_state ps ON ps.patient_program_id = pp.patient_program_id AND ps.voided = 0 AND ps.state = 7 AND ps.start_date <= DATE('#{end_date}')
177
+ INNER JOIN patient_state #{current_partition} ps ON ps.patient_program_id = pp.patient_program_id AND ps.voided = 0 AND ps.state = 7 AND ps.start_date <= DATE('#{end_date}')
178
178
  WHERE o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0
179
179
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}#{' '}
180
180
  AND o.obs_datetime <= '#{end_date}' AND o.obs_datetime >= '#{start_date}'
181
181
  GROUP BY o.person_id
182
182
  ) current_cd4 ON current_cd4.person_id = pp.patient_id
183
- LEFT JOIN obs o ON o.person_id = pp.patient_id AND o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0 AND o.obs_datetime = current_cd4.obs_datetime
183
+ LEFT JOIN obs #{current_partition} o ON o.person_id = pp.patient_id AND o.concept_id = #{concept_name('CD4 count').concept_id} AND o.voided = 0 AND o.obs_datetime = current_cd4.obs_datetime
184
184
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
185
- LEFT JOIN obs transfer_in ON transfer_in.person_id = pp.patient_id
185
+ LEFT JOIN obs #{current_partition} transfer_in ON transfer_in.person_id = pp.patient_id
186
186
  AND transfer_in.concept_id = #{concept_name('Ever registered at ART clinic').concept_id}
187
187
  AND transfer_in.voided = 0
188
188
  AND transfer_in.value_coded = #{concept_name('Yes').concept_id}
189
189
  AND transfer_in.obs_datetime <= '#{end_date}'
190
190
  AND transfer_in.obs_datetime >= '#{start_date}'
191
191
  #{site_manager(operator: 'AND', column: 'transfer_in.site_id', location: @location)}
192
- LEFT JOIN obs pregnant_or_breastfeeding ON pregnant_or_breastfeeding.person_id = pp.patient_id
192
+ LEFT JOIN obs #{current_partition} pregnant_or_breastfeeding ON pregnant_or_breastfeeding.person_id = pp.patient_id
193
193
  AND pregnant_or_breastfeeding.concept_id IN (SELECT concept_id FROM concept_name WHERE name IN ('Breast feeding?', 'Breast feeding', 'Breastfeeding', 'Is patient pregnant?', 'patient pregnant') AND voided = 0)
194
194
  AND pregnant_or_breastfeeding.voided = 0
195
195
  AND pregnant_or_breastfeeding.value_coded = #{concept_name('Yes').concept_id}
@@ -163,11 +163,11 @@ module MalawiHivProgramReports
163
163
  WHEN cd4_result.value_numeric > 200 THEN 'cd4_greater_than_or_equal_to_200'
164
164
  ELSE 'unknown_cd4_count'
165
165
  END cd4_count_group
166
- FROM patient_program
167
- INNER JOIN person ON person.person_id = patient_program.patient_id
166
+ FROM patient_program #{current_partition}
167
+ INNER JOIN person #{current_partition} ON person.person_id = patient_program.patient_id
168
168
  #{site_manager(operator: 'AND', column: 'person.site_id', location: @location)}
169
169
  /* Select patients that were on treatment before start of reporting period */
170
- INNER JOIN patient_state AS patient_ever_on_treatment
170
+ INNER JOIN patient_state #{current_partition} AS patient_ever_on_treatment
171
171
  ON patient_ever_on_treatment.patient_program_id = patient_program.patient_program_id
172
172
  AND patient_ever_on_treatment.state = 7
173
173
  AND patient_ever_on_treatment.start_date < DATE(#{start_date})
@@ -175,8 +175,8 @@ module MalawiHivProgramReports
175
175
  /* Get patient's state at the start of the quarter. */
176
176
  INNER JOIN (
177
177
  SELECT patient_program_id, MAX(patient_state.date_created) AS date_created
178
- FROM patient_state
179
- INNER JOIN patient_program USING (patient_program_id)
178
+ FROM patient_state #{current_partition}
179
+ INNER JOIN patient_program #{current_partition} USING (patient_program_id)
180
180
  WHERE patient_state.voided = 0
181
181
  AND patient_program.voided = 0
182
182
  AND patient_program.program_id = 1
@@ -184,7 +184,7 @@ module MalawiHivProgramReports
184
184
  GROUP BY patient_program_id
185
185
  ) AS date_of_last_patient_state_before_quarter
186
186
  ON date_of_last_patient_state_before_quarter.patient_program_id = patient_program.patient_program_id
187
- LEFT JOIN patient_state AS patient_state_at_start_of_quarter
187
+ LEFT JOIN patient_state #{current_partition} AS patient_state_at_start_of_quarter
188
188
  ON patient_state_at_start_of_quarter.patient_program_id = date_of_last_patient_state_before_quarter.patient_program_id
189
189
  #{site_manager(operator: 'AND', column: 'patient_state_at_start_of_quarter.site_id', location: @location)}
190
190
  AND patient_state_at_start_of_quarter.date_created = date_of_last_patient_state_before_quarter.date_created
@@ -192,15 +192,15 @@ module MalawiHivProgramReports
192
192
  /* Select patients who received ART within the reporting period. */
193
193
  INNER JOIN (
194
194
  SELECT DISTINCT encounter.patient_id, orders.start_date
195
- FROM encounter
196
- INNER JOIN orders
195
+ FROM encounter #{current_partition}
196
+ INNER JOIN orders #{current_partition}
197
197
  ON orders.encounter_id = encounter.encounter_id
198
198
  AND DATE(orders.start_date) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
199
199
  AND orders.voided = 0
200
- INNER JOIN drug_order
200
+ INNER JOIN drug_order #{current_partition}
201
201
  ON drug_order.order_id = orders.order_id
202
202
  AND drug_order.quantity > 0
203
- AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug)
203
+ AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug #{current_partition})
204
204
  WHERE encounter.voided = 0
205
205
  AND encounter.program_id = 1
206
206
  AND DATE(encounter.encounter_datetime) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
@@ -209,8 +209,8 @@ module MalawiHivProgramReports
209
209
  /* Ensure that patients are on ART at the end of the quarter */
210
210
  INNER JOIN (
211
211
  SELECT patient_program_id, MAX(patient_state.date_created) AS date_created
212
- FROM patient_state
213
- INNER JOIN patient_program USING (patient_program_id)
212
+ FROM patient_state #{current_partition}
213
+ INNER JOIN patient_program #{current_partition} USING (patient_program_id)
214
214
  WHERE patient_state.voided = 0
215
215
  AND patient_program.voided = 0
216
216
  AND patient_program.program_id = 1
@@ -220,7 +220,7 @@ module MalawiHivProgramReports
220
220
  ON date_of_last_patient_state_in_quarter.patient_program_id = patient_program.patient_program_id
221
221
 
222
222
  /*Not sure why Walter had this section in but I believe its not neccessary*/
223
- /*INNER JOIN patient_state AS patient_state_at_end_of_quarter
223
+ /*INNER JOIN patient_state #{current_partition} AS patient_state_at_end_of_quarter
224
224
  ON patient_state_at_end_of_quarter.patient_program_id = patient_program.patient_program_id
225
225
  AND patient_state_at_end_of_quarter.date_created = date_of_last_patient_state_before_quarter.date_created
226
226
  AND patient_state_at_end_of_quarter.state = 7*/
@@ -231,17 +231,17 @@ module MalawiHivProgramReports
231
231
  the current_defaulter function. */
232
232
  LEFT JOIN (
233
233
  SELECT DISTINCT encounter.patient_id
234
- FROM encounter
235
- INNER JOIN orders
234
+ FROM encounter #{current_partition}
235
+ INNER JOIN orders #{current_partition}
236
236
  ON orders.encounter_id = encounter.encounter_id
237
237
  AND orders.voided = 0
238
238
  #{site_manager(operator: 'AND', column: 'orders.site_id', location: @location)}
239
239
  AND DATE(orders.start_date) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
240
240
  AND DATE(orders.auto_expire_date) >= (DATE(#{end_date}) - INTERVAL 30 DAY)
241
- INNER JOIN drug_order
241
+ INNER JOIN drug_order #{current_partition}
242
242
  ON drug_order.order_id = orders.order_id
243
243
  AND drug_order.quantity > 0
244
- AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug)
244
+ AND drug_order.drug_inventory_id IN (SELECT DISTINCT drug_id FROM arv_drug #{current_partition})
245
245
  WHERE encounter.program_id = 1
246
246
  AND DATE(encounter.encounter_datetime) BETWEEN DATE(#{start_date}) AND DATE(#{end_date})
247
247
  AND encounter.voided = 0
@@ -249,14 +249,14 @@ module MalawiHivProgramReports
249
249
  ON patients_with_orders_at_end_of_quarter.patient_id = patient_program.patient_id
250
250
  LEFT JOIN (
251
251
  SELECT max(o.obs_datetime) AS obs_datetime, o.person_id
252
- FROM obs o
252
+ FROM obs #{current_partition} o
253
253
  INNER JOIN concept_name cn ON cn.concept_id = o.concept_id AND cn.name = 'CD4 count'
254
254
  #{site_manager(operator: 'AND', column: 'o.site_id', location: @location)}
255
255
  WHERE o.concept_id = #{concept('CD4 count').concept_id} AND o.voided = 0
256
256
  AND o.obs_datetime <= #{end_date} AND o.obs_datetime >= #{start_date}
257
257
  GROUP BY o.person_id
258
258
  ) current_cd4 ON current_cd4.person_id = patient_program.patient_id
259
- LEFT JOIN obs cd4_result ON cd4_result.person_id = patient_program.patient_id AND cd4_result.concept_id = #{concept('CD4 count').concept_id} AND cd4_result.voided = 0
259
+ LEFT JOIN obs #{current_partition} cd4_result ON cd4_result.person_id = patient_program.patient_id AND cd4_result.concept_id = #{concept('CD4 count').concept_id} AND cd4_result.voided = 0
260
260
  #{site_manager(operator: 'AND', column: 'cd4_result.site_id', location: @location)}
261
261
  # Not sure why we are matching the two dates but the result was excluding cd4 results for patients.
262
262
  # AND cd4_result.obs_datetime = current_cd4.obs_datetime
@@ -266,10 +266,10 @@ module MalawiHivProgramReports
266
266
  before the start of the reporting period */
267
267
  AND patient_program.patient_id NOT IN (
268
268
  SELECT DISTINCT orders.patient_id
269
- FROM orders
270
- INNER JOIN drug_order USING (order_id)
269
+ FROM orders #{current_partition}
270
+ INNER JOIN drug_order #{current_partition} USING (order_id)
271
271
  INNER JOIN arv_drug ON arv_drug.drug_id = drug_inventory_id
272
- INNER JOIN patient_program
272
+ INNER JOIN patient_program #{current_partition}
273
273
  ON patient_program.patient_id = orders.patient_id
274
274
  AND patient_program.program_id = 1
275
275
  #{site_manager(operator: 'AND', column: 'orders.site_id', location: @location)}