我希望我可以关联“内联视图”

时间:2009-11-13 16:31:05

标签: sql sql-server syntax derived-table inline-view

我有一张病人表:

PatientId   Admitted
---------   ---------------
1           d/m/yy hh:mm:ss
2           d/m/yy hh:mm:ss
3           d/m/yy hh:mm:ss

我有一个PatientMeasurement表(0到很多):

PatientId   MeasurementId   Recorded            Value
---------   -------------   ---------------     -----
1           A               d/h/yy hh:mm:ss     100
1           A               d/h/yy hh:mm:ss     200
1           A               d/h/yy hh:mm:ss     300
2           A               d/h/yy hh:mm:ss     10
2           A               d/h/yy hh:mm:ss     20
1           B               d/h/yy hh:mm:ss     1
1           B               d/h/yy hh:mm:ss     2

我正在尝试创建一个类似于

的结果集
PatientId   Numerator   Denominator
---------   --------    -----------
1           1           1
2           1           1
3           0           1       

基本上,如果患者至少有一个测量值A和一个测量值B,患者的分子中将有一个1。在这个例子中,患者1有3个A测量和2个B测量,所以分子是1.患者2有2 A测量,但没有B测量,因此分子为0.患者既没有A测量也没有B测量,所以分子是0。

到目前为止,我的查询是:

SELECT  PatientId, CASE WHEN a.cnt+b.cnt>2 THEN 1 ELSE 0 END Numerator, 1 Denominator
FROM    patient p

LEFT OUTER JOIN (
    SELECT  PatientId, count(*) cnt
    FROM    PatientMeasurement pm
    WHERE   MeasurementId='A'
    --AND   Recorded <= dateadd(hh, 12, Admitted)
    GROUP BY PatientId
) a ON p.PatientId=a.PatientId

LEFT OUTER JOIN (
    SELECT  PatientId, count(*) cnt
    FROM    PatientMeasurement pm
    WHERE   MeasurementId='B'
    --AND   Recorded <= dateadd(hh, 12, Admitted)
    GROUP BY PatientId
) b ON p.PatientId=b.PatientId

只要我不包含相关的日期限制(Recorded&lt; dateadd(hh,12,Admitted)),这就按预期工作。不幸的是,以这种方式关联'内联视图'在语法上并不合适。< / p>

这迫使我将SQL重写为:

SELECT  PatientId, CASE WHEN v.a+v.b>2 THEN 1 ELSE 0 END Numerator, 1 Denominator
FROM    (

    SELECT  PatientId,
    (
        SELECT  PatientId, count(*) cnt
        FROM    PatientMeasurement pm
        WHERE   PatientId=p.PatientId
        AND MeasurementId='A'
        AND Recorded <= dateadd(hh, 12, Admitted)
        GROUP BY PatientId
    ) a,
    (
        SELECT  PatientId, count(*) cnt
        FROM    PatientMeasurement pm
        WHERE   PatientId=p.PatientId
        AND MeasurementId='B'
        AND Recorded <= dateadd(hh, 12, Admitted)
        GROUP BY PatientId
    ) b
    FROM    Patient p
) v

我的问题:有更好,更有效的方法吗?

感谢您的时间。

5 个答案:

答案 0 :(得分:1)

试试这个:

WITH GroupPatients AS 
    (SELECT MeasurementID, PatientId, Count(*) AS cnt
    FROM PatientMeasurement AS pm
    INNER JOIN Patient p ON pm.PatientID = p.PatientID
    WHERE
        MeasurementId IN ('A', 'B')
    AND
        Recorded <= dateadd(hh, 12, Admitted)
    GROUP BY MeasureMentID, PatientId)

SELECT p.PatientID, Case
    When IsNull(GPA.cnt, 0) > 0 AND IsNull(GPB.cnt, 0) > 0 Then 1
    Else 0
End AS Numerator, 1 AS Denominator
FROM Patient p
LEFT JOIN GroupPatientsA AS GPA ON p.PatientID = GPA.PatientID AND GPA.MeasurementID = 'A'
LEFT JOIN GroupPatientsB AS GPB ON p.PatientID = GPB.PatientID AND GPB.MeasurementID = 'B'

我也对业务逻辑进行了一次调整 - 你的规范说如果患者同时进行A和B测量,则Numerator应为1 - 但是,a.cnt + b.cnt&gt; 2的条款将错误地返回如果a.cnt或b.cnt为3或更多而另一个为零。

答案 1 :(得分:1)

SELECT  p.*, 
        CASE WHEN
        EXISTS
        (
        SELECT  NULL
        FROM    PatientMeasurement pm
        WHERE   pm.PatientID = p.ID
                AND pm.Type = 'A'
                AND pm.Recorded <= DATEADD(hh, 12, p.Admitted)
        ) AND EXISTS (
        SELECT  NULL
        FROM    PatientMeasurement pm
        WHERE   pm.PatientID = p.ID
                AND pm.Type = 'B'
                AND pm.Recorded <= DATEADD(hh, 12, p.Admitted)
        ) THEN 1 ELSE 0 END
FROM    Patient p

答案 2 :(得分:1)

另一种解决方案可以使用OUTER APPLY

接近原始尝试
SELECT  PatientId, CASE WHEN a.cnt+b.cnt>2 THEN 1 ELSE 0 END Numerator, 1 Denominator 
FROM    patient p 
OUTER APPLY ( 
    SELECT      count(*) cnt 
    FROM        PatientMeasurement pm 
    WHERE       MeasurementId='A' 
    AND       Recorded <= dateadd(hh, 12, p.Admitted) 
    AND pm.PatientId = p.PatientId
) AS a(cnt)     
OUTER APPLY ( 
    SELECT      count(*) cnt 
    FROM        PatientMeasurement pm 
    WHERE       MeasurementId='B' 
    AND       Recorded <= dateadd(hh, 12, p.Admitted) 
    AND pm.PatientId = p.PatientId
) AS b(cnt)

答案 3 :(得分:0)

假设您使用的是Sql 2005或2008,可以使用一些窗口函数和数据透视表简化整个查询:

with pData as
(
    select  count(*) over(partition by PatientId, MeasurementId) as cnt,
            PatientId, MeasurementId
    from    PatientMeasurement pm
    where   MeasurementId in('A','B')
    and     Recorded <= dateadd(hh, 12, Admitted)
)
select  PatientId, coalesce([A],0) as cntA, coalesce([B],0) as cntB,
        case when coalesce([A],0) + coalesce([B],0) > 2 then 1 else 0 end as Numerator,
        1 as Denominator
from    pData
pivot   (max(cnt) for MeasurementId in([A],[B])) pvt

答案 4 :(得分:0)

DECLARE @TimeSlot int;
SET @TimeSlot = 12;

WITH 
pt AS (
    SELECT p.PatientID, p.Admitted, m.MeasurementID, m.Recorded,
        CASE 
          WHEN m.Recorded <= dateadd(hh, @TimeSlot, p.Admitted) THEN 1 
          ELSE 0 
        END AS "InTimeSlot"
    FROM Patient AS p
    LEFT JOIN PatientMeasurement AS m ON p.PatientID = m.PatientID
),
cntA AS (
    SELECT PatientID, count(*) AS "A_count"
    FROM pt WHERE MeasurementID='A' AND InTimeSlot = 1
    GROUP BY PatientID
),
cntB AS (
    SELECT PatientID, count(*) AS "B_count"
    FROM pt WHERE MeasurementID='B' AND InTimeSlot = 1
    GROUP BY PatientID
),
cntAB AS (
    SELECT p.PatientID
          ,coalesce(a.A_count, 0) AS "A_cnt"
          ,coalesce(b.B_count, 0) AS "B_cnt"
    FROM Patient as p
    LEFT JOIN  cntA  AS a ON p.PatientID = a.PatientID
    LEFT JOIN  cntB  AS b ON p.PatientID = b.PatientID
),
cntN AS (
    SELECT PatientID,
        CASE WHEN A_cnt > 0 AND B_cnt > 0 THEN 1 ELSE 0 END AS Numerator
    FROM cntAB 
)
SELECT PatientID, Numerator, 1 AS Denominator FROM cntN