I am in quite the pickle! I am checking a table that contains information related to patient vaccines. For each patient, there are three possible vaccines stored in four rows. These vaccines have a related yes/no value that depicts whether or not the vaccine was in effect. These vaccine rows each had a ‘static’ de_name column that held the name of the vaccine that was potentially being administered. If the vaccine had a yes value, there is also a related row that holds a date value of when the vaccine was administered. Luckily, this second row containing dates also contained the name of the field, which could be used to deduce which vaccine was administered. Due to the row nature of this table, there was no obvious way (to me) to search through each patient other than to turn each possibility into it’s own column and coalesce the results. This allowed me to create two pretty columns for both the administered vaccines and the date the vaccines were administered. This worked because I did not need to reference the rows that pertained to the yes/no value because the name of the vaccine and the fact that the date existed allowed me to use just the date field itself.
Everything was fine and dandy until I was asked to add a fourth vaccine, which instead of holding a yes/no value, contained free-text. I was asked to place the free-text in the same column as the other vaccines ‘static’ text. This is an issue because unlike the rest of the vaccines, there was no column in the fourth vaccines date row that displayed the non-date row vaccine free-text entry. I had to reference the fourth row’s free-text while also referencing the fourth row’s related vaccine date row’s date value. The issue comes in when, I have no idea how to join to rows (by a permanent visit number) within a Case statement within a coalesce statement! Any help would be wonderful!
Please let me know if there is any other data you need me to include. I am including apprehensively some dummy data and what my query would currently display.
Data_Points Table:
De_Num De_Name De_Value Perm_Visit_Num PatientID
6224 Flu vaccine yes 10111 12
6225 Flu vaccine date administered 11/00/2012 10111 12
6226 Pneumonia vaccine yes 10112 12
6227 Pneumonia vaccine date administered 11/01/2012 10112 12
6228 Tetanus vaccine yes 10113 12
6229 Tetanus vaccine date administered 11/02/2012 10113 12
6230 Other vaccine arbitrary free-text 10114 12
6231 Other vaccine date administered 11/03/2012 10114 12
6224 Flu vaccine yes 10115 10
6225 Flu vaccine date administered 11/00/2012 10115 10
6226 Pneumonia vaccine yes 10116 10
6227 Pneumonia vaccine date administered 11/01/2012 10116 10
6228 Tetanus vaccine yes 10117 10
6229 Tetanus vaccine date administered 11/02/2012 10117 10
6230 Other vaccine more arbitrary free-text 10118 10
6231 Other vaccine date administered 11/03/2012 10118 10
My Query
select *
from
(select
COALESCE(
CASE
WHEN de_num = '6225' THEN DE_Name
END,
CASE
WHEN de_num = '6227' THEN DE_Name
END,
CASE
WHEN de_num = '6229' and perm_visit_num in (select perm_visit_num from data_points where de_num = '6228' and de_value = 'Yes') THEN de_name
END,
CASE
WHEN de_num = '6230' THEN DE_Value
END
) VaccineAdministered,
COALESCE(
CASE
WHEN de_num = '6225' THEN CASE DE_Value WHEN null THEN null ELSE DE_Value END
END,
CASE
WHEN de_num = '6227' THEN CASE DE_Value WHEN null THEN null ELSE DE_Value END
END,
CASE
WHEN de_num = '6229' and perm_visit_num in (select perm_visit_num from data_points where de_num = '6228' and de_value = 'Yes') THEN CASE DE_Value WHEN null THEN null ELSE DE_Value END
END,
CASE
WHEN de_num = '6230' THEN 'foo'
END
)
from data_points
WHERE DE_Num in ('6227','6225','6229','6230')
)
where vaccineadministered != ''
The result set with that dummy data and the current sql query above:
Vaccine Administered DateAdministered
Flu vaccine date administered 11/00/2012
Pneumonia vaccine date administered 11/01/2012
Tetanus vaccine date administered 11/02/2012
arbitrary free-text foo
Flu vaccine date administered 11/00/2012
Pneumonia vaccine date administered 11/01/2012
Tetanus vaccine date administered 11/02/2012
more arbitrary free-text foo
My issue is that I cannot figure out a way to pull both the free-text AND the associated date. I can get either or, and in this query I get the free-text.
Please understand this is a very hard thing to explain and I am really in need of help. Thanks!
After further reflection, this is what you need. I initially thought de_num was a primary key but I assume it is some kind of entry type. This isn’t such a bad structure then:
This joins the vaccine record, with the administered date record in all cases. You can twiddle with the de_nums in the first part if you want to display the other values.
This assumes that each perm_visit_num only has two entries. If you can get two ‘other vaccines’ in one visit, then this structure just plain can’t cope.
http://sqlfiddle.com/#!3/4a8dd/16/0