次のクエリは、病院に行った人の数、病院に行った人の総数、およびこれら 2 つを割ってパーセンテージを求めるように設計されています。テーブルClaims
は 200 万以上の行であり、正しい非クラスター化インデックス がありpatientid, admissiondate, and dischargdate
ます。クエリは十分に速く実行されますが、より使いやすくする方法に興味があります。行に別のコードを追加し(hcpcs.hcpcs ='97001')
、変更をpercentRehabNotHomeHealth
別の列に反映できるようにしたいと考えています。2 つのクエリの結果を結合する、大きくて分厚い結合ステートメントを書かなくても可能ですか? 余分な列を追加すると、数学が正しく見えなくなることはわかっていますが、現時点では心配していません。必要なサンプル出力: http://imgur.com/BCLrd
データベース スキーマ
select h.hospitalname
,count(*) as visitCounts
,hospitalcounts
,round(count(*)/cast(hospitalcounts as float) *100,2) as percentRehabNotHomeHealth
from Patient p
inner join statecounties as sc on sc.countycode = p.countycode
and sc.statecode = p.statecode
inner join hospitals as h on h.npi=p.hospitalnpi
inner join
--this join adds the hospitalCounts column
(
select h.hospitalname, count(*) as hospitalCounts
from hospitals as h
inner join patient as p on p.hospitalnpi=h.npi
where p.statecode='21' and h.statecode='21'
group by h.hospitalname
) as t on t.hospitalname=h.hospitalname
--this where exists clause gives the visitCounts column
where h.stateCode='21' and p.statecode='21'
and exists
(
select distinct p2.patientid
from Patient as p2
inner join Claims as c on c.patientid = p2.patientid
and c.admissiondate = p2.admissiondate
and c.dischargedate = p2.dischargedate
inner join hcpcs on hcpcs.hcpcs=c.hcpcs
inner join hospitals as h on h.npi=p2.hospitalnpi
where (hcpcs.hcpcs ='97001' or hcpcs.hcpcs='9339' or hcpcs.hcpcs='97002')
and p2.patientid=p.patientid
)
and hospitalcounts > 10
group by h.hospitalname, t.hospitalcounts
having count(*)>10