简体   繁体   中英

Ad hoc 2x2 contingency tables SQL Server 2008

I'm looking at ways of creating contingency tables in SQL Server 2008. They don't necessarily have to appear in the 2x2 typical matrix. I'd just like to know if anyone out there can see a better solution than mine.

在此处输入图片说明 Please refer to the picture for clarity. The red letters are names of the squares for simplicity's sake. The label X+ means X is present in that cell and the opposite is true as well.

I will label my queries with the letter of box in the table that they represent

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 This one I'm a little iffy about but I think I'm going to do something like

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

This aims to find the entire population and subtracting those ONLY with X and ONLY with y

Yes! There are easier ways. Assuming your join produces no duplicate patients:

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)

Otherwise replace the count(*) with:

count(distinct patientid)

This should give you the information you need for the contingency table.

Here's an alternate way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM