临时 2x2 列联表 SQL Server 2008

Ad hoc 2x2 contingency tables SQL Server 2008(临时 2x2 列联表 SQL Server 2008)
本文介绍了临时 2x2 列联表 SQL Server 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

为清楚起见,请参阅图片.为简单起见,红色字母是方块的名称.标签 X+ 表示 X 存在于该单元格中,反之亦然.

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这个我有点不确定,但我想我会做类似的事情

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

这旨在找到整个人口并仅用 X 和仅用 y 减去那些

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)

否则将 count(*) 替换为:

Otherwise replace the count(*) with:

count(distinct patientid)

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

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

这篇关于临时 2x2 列联表 SQL Server 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)