Ad hoc 2x2列联表SQL Server 2008

时间:2012-08-27 19:51:38

标签: sql sql-server tsql statistics

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

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减去那些,而只用y

2 个答案:

答案 0 :(得分: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 :(得分:0)

这是另一种方式:

.triangle-border {
  position:relative;
  padding:15px;
  margin:1em 0 .5em;
  border:1px solid #5a8f00;
  color:#333;
  background:#fff;
  /* css3 */
  -webkit-border-radius:10px;
  -moz-border-radius:10px;
  border-radius:10px;
  min-width: 90%;
  max-width: 90%;
  background-color: #E0F6F2;
}

.triangle-border.blue {
  background-color: #CDE5F7;
  border:1px solid #3A7DBA;
}

.triangle-border.right {
  margin-right:30px;
}

.triangle-border:before {
  content:"";
  position:absolute;
  bottom:-20px; /* value = - border-top-width - border-bottom-width */
  left:40px; /* controls horizontal position */
  border-width:20px 20px 0;
  border-style:solid;
  border-color:#5a8f00 transparent;
  /* reduce the damage in FF3.0 */
  display:block;
  width:0;
}

/* creates the smaller  triangle */
.triangle-border:after {
  content:"";
  position:absolute;
  bottom:-13px; /* value = - border-top-width - border-bottom-width */
  left:47px; /* value = (:before left) + (:before border-left) - (:after border-left) */
  border-width:13px 13px 0;
  border-style:solid;
  border-color:#fff transparent;
  /* reduce the damage in FF3.0 */
  display:block;
  width:0;
}

/* creates the larger triangle */
.triangle-border.right:before {
  top:10px; /* controls vertical position */
  bottom:auto;
  left:auto;
  right:-30px; /* value = - border-left-width - border-right-width */
  border-width:15px 0 15px 30px;
  border-color:transparent #3A7DBA;
}

/* creates the smaller  triangle */
.triangle-border.right:after {
  top:16px; /* value = (:before top) + (:before border-top) - (:after border-top) */
  bottom:auto;
  left:auto;
  right:-21px; /* value = - border-left-width - border-right-width */
  border-width:9px 0 9px 21px;
  border-color:transparent #fff;
}

.pagecontent .cols .col.two .sign-in {
  table-layout: fixed;
  display: table; 
  outline: none;
}

.pagecontent .cols .col.two .sign-in .cell {
  display: table-cell;
  margin: 5px;
  height: 100%;
}

.pagecontent .cols .col.two .sign-in .cell.one {
  width: 85%;
}

.pagecontent .cols .col.two .sign-in .cell.two {
  padding-left: 2%;
  width: 15%;
}

.pagecontent span.si {
  font-size: .8em;
  color: #808C8D;
}