sql - 使用大表的 WHERE EXISTS 查询可扩展性

标签 sql sql-server-2008 tsql

以下查询旨在查找去医院的人数、去医院的总人数,然后将这两者相除以获得百分比。表Claims超过 200 万行,并且确实具有正确的非聚集索引 patientid, admissiondate, and dischargdate .查询运行得足够快,但我对如何使它更有用很感兴趣。我希望能够在 (hcpcs.hcpcs ='97001') 的行中添加另一个代码并在 percentRehabNotHomeHealth 中进行更改在另一列中得到反射(reflect)。有没有可能不编写一个大的、胖的连接语句来将两个查询的结果连接在一起?我知道通过添加额外的列,数学看起来不正确,但我现在并不担心。期望的样本输出:http://imgur.com/BCLrd
数据库模式

enter image description here

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

最佳答案

您可能会查看 CTE(通用表表达式)来获得所需的内容。它将允许您获取汇总数据并将其加入到公共(public) key 的详细信息中。例如,我将您对子查询的联接修改为 CTE。

;with hospitalCounts as (
    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
)
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 hospitalCounts on t.hospitalname=h.hospitalname
--this where exists clause gives the visitCounts column
where h.stateCode='21' and p.statecode='21'
and exists
(
    select 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

关于sql - 使用大表的 WHERE EXISTS 查询可扩展性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11785693/

相关文章:

sql-server - 如何将分钟数转换为 hh :mm format in TSQL?

.net - Umbraco中的数据库连接初始化失败

sql - sql中的日期时间比较错误?

java - spring batch 中来自 oracle 数据库的结果集的分区器

c# - 无法使用 SqlConnection 连接到任何远程数据库

sql-server - 创建笛卡尔积时,CROSS APPLY 和 OUTER APPLY 有什么区别吗?

sql - 是否可以在 VB6 中使用 SQL 查询填充 Farpoint Spread 6.0 vaSpread 组件?

mysql - 从 MS SQL Server 插入到 MySQL 数据库

sql - 在 Oracle SQL 中删除 HTML 标签的更好方法

sql - 如何仅显示查询中字段的前 5 位数字