sql - Mapping rows to columns in different tables -
i stumped how elegantly map data rows columns in other tables.
input table
form_submission
table -
id
= form identifier entry_id
= question identifier response
= response
id entry_id response 4 24 john 4 25 doe 4 26 male 4 32 ny 4 30 life-threatening 4 30 other serious 4 30 hospitalization 4 28 tylenol 4 31 have headache.
i need map couple tables patient_info
, patient_outcome
output tables
patient_info
report_id first_name last_name state gender complaint 4 john doe ny male have headache.
patient_outcome
report_id other_serious_ind life_threat_ind hospital_ind disability_ind 4 y y y n
so there no direct way link rows columns, possible create mapping based on entry_id
, column name? though know ind
columns based on rows entry_id
= 30.
it's possible conditional aggregation. but, in end, need still need figure out rules mappings correctly.
here sample sql based on basic understanding of how mapping works. you'll need tweak further.
patient_info
select id report_id, max(case when entry_id = 24 response end) first_name, max(case when entry_id = 25 response end) last_name, max(case when entry_id = 32 response end) state, max(case when entry_id = 26 response end) gender, max(case when entry_id = 31 response end) complaint form_submission group id
patient_outcome
select id report_id, nvl(max(case when entry_id = 30 , response = 'other serious' 'y' end), 'n') other_serious_ind, nvl(max(case when entry_id = 30 , response = 'life-threatening' 'y' end), 'n') life_threat_ind, nvl(max(case when entry_id = 30 , response = 'hospitalization' 'y' end), 'n') hospital_ind, nvl(max(case when entry_id = 30 , response = '??disability??' 'y' end), 'n') disability_ind form_submission group id
Comments
Post a Comment