I am running the following query and need to modify it to pull only 1 row, with the most recent Onset Date, when the pr.diagnosis_code_id and pr.description are not unique. I could actually use one or the other, since the code always maps to the same description.
Currently, my query outputs:
Acute cholecystitis, 575.0, 06/29/2010, --, --
Acute pancreatitis, 577.0, 06/08/2010, --, --
Acute posthemorrhagic anemia, 285.1, 06/29/2010, --, --
Bipolar Disoder, 296.80, --, --, 06/29/2010
Bipolar Disoder, 296.80, 06/08/2010, --, 06/29/2010
ETOH Dependence, 303.91, 06/08/2010, --, --
And I want it to output the following (removing the Bipolar Disoder where there the Onset date is the least current), but I don't know how to do a "distinct" on the pr.description or pr.diagnosis_code_id:
Acute cholecystitis, 575.0, 06/29/2010, --, --
Acute pancreatitis, 577.0, 06/08/2010, --, --
Acute posthemorrhagic anemia, 285.1, 06/29/2010, --, --
Bipolar Disoder, 296.80, 06/08/2010, --, 06/29/2010
ETOH Dependence, 303.91, 06/08/2010, --, --
Here is my query:
select
pr.diagnosis_code_id,
pr.description,
pr.date_onset_sympt as dt1,
pr.date_diagnosed as dt2,
pr.date_resolved as dt3,
pe.enc_timestamp as dt4
from patient p,
patient_diagnosis pr,
patient_encounter pe
where p.med_rec_nbr = $mrn
and pr.person_id = p.person_id
and pr.practice_id = $practice_id
and pr.enterprise_id = $enterprise_id
and pe.enc_id = pr.enc_id
order by pr.description, pe.enc_timestamp DESC
Thanks! I am new to this and really struggle once I get out of the simple select stuff!