Thanks to the people who answered my question this morning:
This is an extension of it and I'm not clear how to generalize the previous solutions to this case.
The database I'm working with has medications, lab values, and diagnoses for a set of patients in several tables. Similar to the above question:
For years x = 1996 to 2008:
I want to count the number of patients that had a specific medication prescribed in year x, a specific lab value taken in year x, (this last one is a little different!) and a specific diagnosis first given in 1996 or later, which has not yet been resolved in year x.
EDIT: It turns out that pt_id is not a primary key in any of the tables - from what I've been reading SELECT COUNT DISTINCT(pm.pt_id)
(because of DISTINCT
being slow) but I would be fine with a solution that used distinct if it worked.
Here is the query for year 2008 alone, but again I want a query that will count the values for each year from 1996 to 2008.
SELECT COUNT (pm.pt_id)
FROM dm.medications pm
/*patient was prescribed statins together with fibrates this year*/
WHERE pm.pt_id IN
(
SELECT statins.pt_id
FROM dm.patient_medications statins
INNER JOIN dm.patient_medications other_meds
ON statins.pt_id = other_meds.pt_id
WHERE Year(other_meds.order_dts) = 2008
AND Year(statins.order_dts) = 2008
AND statins.generic_nm in ('Atorvastatin','Cerivastatin')
AND other_meds.generic_nm in ('Clofibrate','Fenofibrate','Gemfibrozil')
)
/* patient had a diagnosis code in the list first diagnosed in 1996 or later and not yet resolved in this year */
WHERE pm.pt_id in
(
SELECT pd.pt_id,
FROM dm.diagnoses pd
WHERE pd.icd9_cd IN('728.89','729.1','710.4','728.3','729.0','728.81','781.0','791.3')
AND Year(pd.init_noted_dts) >= 1996
AND pd.rslvd_dts IS NOT NULL
AND Year(pd.rslvd_dts) >= 2008
)
/* patient had a lab value above 1000 this year */
AND pm.pt_id IN
(
SELECT pl.pt_id
FROM dm.labs pl
WHERE pl.lab_val > 1000
AND pl.lab_val IS NOT NULL
AND pl.lab_val < 999999
AND pl.lab_nm = 'CK (CPK)'
AND Year(pm.order_dts) = 2008
)
/* we have demographic information about this patient */
AND pm.pt_id IN
(
SELECT p.pt_id
FROM mrd.demographics p
)
/* this is a real person */
AND pm.pt_id IS NOT NULL