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 に答える