2012-08-27 23 views
1

我正在尋找在SQL Server 2008中創建contingency tables的方法。它們不一定必須出現在2x2典型的矩陣中。我只想知道有沒有人能看到比我更好的解決方案。特設2x2應急表SQL Server 2008

enter image description here 請參考圖片的清晰。爲了簡單起見,紅色字母是正方形的名稱。標籤X +表示X存在於該單元格中,反之亦然。

我將標籤我的查詢框中的表中的字母,它們代表

A 

     select count(*) from 
    (

    select distinct p.patientid 
     from Patient as p 
     inner join icdpatient as picd on picd.patientid = p.patientid 
     and picd.admissiondate = p.admissiondate 
     and picd.dischargedate = p.dischargedate 
     inner join tblicd as t on t.icd_id = picd.icd_id 
     where t.icdText like '%x%' 
    ) as t 
    inner join 
    (
    select distinct p.patientid 
     from Patient as p 
     inner join icdpatient as picd on picd.patientid = p.patientid 
     and picd.admissiondate = p.admissiondate 
     and picd.dischargedate = p.dischargedate 
     inner join tblicd as t on t.icd_id = picd.icd_id 
     where t.icdText like '%y%' 
    ) as s on s.patientid=t.patientid 

B 
select count(*) from 
(

select distinct p.patientid 
    from Patient as p 
    inner join icdpatient as picd on picd.patientid = p.patientid 
    and picd.admissiondate = p.admissiondate 
    and picd.dischargedate = p.dischargedate 
    inner join tblicd as t on t.icd_id = picd.icd_id 
    where t.icdText like '%x%' 
) as t 
left join 
(
select distinct p.patientid 
    from Patient as p 
    inner join icdpatient as picd on picd.patientid = p.patientid 
    and picd.admissiondate = p.admissiondate 
    and picd.dischargedate = p.dischargedate 
    inner join tblicd as t on t.icd_id = picd.icd_id 
    where t.icdText like '%y%' 
) as s on s.patientid=t.patientid 
where s.patientid is null 

C 
select * from 
(

select distinct p.patientid 
    from Patient as p 
    inner join icdpatient as picd on picd.patientid = p.patientid 
    and picd.admissiondate = p.admissiondate 
    and picd.dischargedate = p.dischargedate 
    inner join tblicd as t on t.icd_id = picd.icd_id 
    where t.icdText like '%x%' 
) as t 
right join 
(
select distinct p.patientid 
    from Patient as p 
    inner join icdpatient as picd on picd.patientid = p.patientid 
    and picd.admissiondate = p.admissiondate 
    and picd.dischargedate = p.dischargedate 
    inner join tblicd as t on t.icd_id = picd.icd_id 
    where t.icdText like '%y%' 
) as s on s.patientid=t.patientid 
where t.patientid is null 

d 這一次我有點玄乎約但我想我會去做點像

declare @d int 
set @d = (select count(distinct p.patientid) from Patient as p) - b -c 

這樣做的目的是找到整個人口,並減去那些只有X和ONLY與y

回答

1

是的!有更簡單的方法。假設你的加入不會產生重複的患者:

select (case when t.icdText like '%x%' then 'X' else 'NO-X' end) as X, 
     (case when t.icdText like '%y%' then 'Y' else 'NO-Y' end) as Y, 
     count(*) as cnt 
from Patient p inner join 
    icdpatient picd 
    on picd.patientid = p.patientid and 
     picd.admissiondate = p.admissiondate and 
     picd.dischargedate = p.dischargedate inner join 
    tblicd t 
    on t.icd_id = picd.icd_id 
group by (case when t.icdText like '%x%' then 'X' else 'NO-X' end), 
      (case when t.icdText like '%y%' then 'Y' else 'NO-Y' end) 

否則替換爲COUNT(*):

count(distinct patientid) 

這應該給你你需要的應急表中的信息。

+1

需要'tblicd as'後別名運行 – wootscootinboogie

+0

@wootscootinboogie。 。 。謝謝。表達「as on」幾乎看起來像一個SQL笑話。 –

+0

我很好地掌握了這裏發生了什麼。當我的連接產生重複項時,如何使用這種方法? – wootscootinboogie

0

這裏的另一種方法:

select  RRTGotAlert   = case RRTGotAlert when 0 then 'N' when 1 then 'Y' end 
      ,TZ_OnPilotUnit_N = sum(TZ_OnPilotUnit_N) 
      ,TZ_OnPilotUnit_Y = sum(TZ_OnPilotUnit_Y) 
from  (select  RRTGotAlert 
         ,TZ_OnPilotUnit_N = [0] 
         ,TZ_OnPilotUnit_Y = [1] 
      from  #analysis 
      pivot  (count(Encounter) for TZ_OnPilotUnit in ([0],[1])) pvt) got_alert 
group by case RRTGotAlert when 0 then 'N' when 1 then 'Y' end 
order by case RRTGotAlert when 0 then 'N' when 1 then 'Y' end