2010-01-18 48 views
1

感謝今天早上誰回答我的問題的人:的SQL Server 2008:計數鍵數目在多個日期範圍跨多個表

SQL Server 2008: Count Number of Keys In Multiple Date Ranges

這是它的一個擴展,我不清楚如何將此前的解決方案概括爲這種情況。

我正在使用的數據庫具有藥物,實驗室值和幾個表格中的一組患者的診斷。與上述類似的問題:

多年來,X = 1996 - 2008年:

我要計數的患者認爲有X年規定的特定藥物的數量,採取了X年特定實驗室值,(這最後一個是有點不同!) 和1996年或以後首次給出的具體診斷,這在第x年還沒有得到解決。

編輯:原來,pt_id是任何表的主鍵 - 從我一直在讀SELECT COUNT DISTINCT(pm.pt_id)(因爲DISTINCT正在緩慢),但我會被罰款與使用不同的解決方案如果它工作。

這裏是2008年單獨查詢,但我又希望有一個查詢,將計算,每年從1996年的值至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 

回答

1

試試這個:再次

SELECT COUNT(*), t1.year 
FROM dm.medications pm 
/*patient was prescribed statins together with fibrates this year*/ 
inner join (
    SELECT statins.pt_id, Year(other_meds.order_dts) as Year 
    FROM dm.patient_medications statins 
    INNER JOIN dm.patient_medications other_meds ON statins.pt_id = other_meds.pt_id 
     AND Year(other_meds.order_dts) = Year(statins.order_dts) 
    WHERE statins.generic_nm in ('Atorvastatin','Cerivastatin') 
     AND other_meds.generic_nm in ('Clofibrate','Fenofibrate','Gemfibrozil') 
) t1 on pm.pt_id = t1.pt_id 
/* patient had a diagnosis code in the list first diagnosed in 1996 or later and not yet resolved in this year */ 
left outer join 
(
    SELECT pd.pt_id, Year(pd.rslvd_dts) as Year 
    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 
) t2 on pm.pt_id = t2.pt_id and t1.Year = t2.Year 
/* patient had a lab value above 1000 this year */ 
inner join (
SELECT pl.pt_id, Year(pm.order_dts) as Year 
    FROM dm.labs pl 
    WHERE pl.lab_val > 1000 
     AND pl.lab_val < 999999 
     AND pl.lab_nm = 'CK (CPK)' 
) t3 on pm.pt_id = t3.pt_id and t2.Year = t3.Year 
/* we have demographic information about this patient */ 
inner join (
    SELECT p.pt_id 
    FROM mrd.demographics p  
) t4 on pm.pt_id = t4.pt_id and t3.Year = t4.Year 
/* this is a real person */ 
where pm.pt_id IS NOT NULL 
    and t1.year between 1996 and 2008 
    and t2.Year is null 
group by t1.year 
+0

謝謝Orbman!看到我上面的編輯(pt_id不是主鍵)。此外,我不確定它是否適用於'AND Year(pm.order_dts)= 2008'和'AND Year(pd.rslvd_dts)> = 2008',因爲每年我只想選擇患有那一年的'order_dts'(可能不是2008年)以及那一年後的某個時間的「resolved_dts」(這可能不再是2008年)。 – raoulcousins 2010-01-19 03:42:28

+0

嘗試修訂版本。 – RedFilter 2010-01-19 14:00:22

+0

嗯。獲取消息207,級別16,狀態1,行32 無效的列名稱「年」。 (pm.pt_id = t4.pt_id和t3.Year = t4.Year'上的't4行),但我沒有看到問題出在哪裏。當我註釋掉最後一個內部連接時,它不會返回任何記錄... – raoulcousins 2010-01-22 06:09:59