1

我正在研究在 SQL Server 2008 中创建列联表的方法。它们不一定必须出现在 2x2 典型矩阵中。我只是想知道是否有人可以看到比我更好的解决方案。

在此处输入图像描述 为了清楚起见,请参阅图片。为简单起见,红色字母是方块的名称。标签 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 和仅用 y 减去那些

4

2 回答 2

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)

这应该为您提供列联表所需的信息。

于 2012-08-27T19:57:16.287 回答
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
于 2015-12-08T22:09:36.740 回答