从多列中选择最大值

时间:2014-09-09 16:01:52

标签: sql sql-server

我正在编写代码来选择在出院后30天内死亡的患者,我的问题是,当我在30天的耐受范围内有多次出院的患者时,它会拉回多排!我尝试使用最大放电日期来解决这个问题,但是当我添加额外的列时,它似乎从其他行中拉出某些元素。这是我的代码:

    SELECT  MAX(IPS.disch_dttm)               [Discharge Datetime]
            ,MAX(IPS.IP_SPELL_ID)             [Spell ID]    
            ,pat.PAS_ID                       [K Number]
            ,MAX(IPS.DIS_WARD_ID)             [Ward ID]
            ,DSSU.SU_DESCRIPTION              [Discharging Ward]

    FROM Pat_spell AS IPS
    LEFT JOIN PATIENT PAT       WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
    LEFT JOIN SPECIALTY SPEC    WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
    LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID

    WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
    AND IPS.DIM_DIS_SPECT_ID = '7195'
    AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'

    GROUP BY pat.PAS_ID
            ,pat.DEATH_DTTM
            ,IPS.DIM_PATIENT_ID
            ,DSSU.SSU_DESCRIPTION

    ORDER BY pat.PAS_ID        

以上是我用于调试的单行代码的输出:

Disch Date  Event_ID   Unique ID  Ward ID   Discharging Ward
2014-06-14  8366113    A123456   77085       WardA 

上面的病房ID是正确的,但&#34;放电病房&#34;是错的。此外,Event_ID与之前的出勤相对应。我想要实现的目标是在死亡后的30天内仅使用事件ID&#39;事件ID&#39;作为我的唯一身份证。如果我想要多行,这就是输出的样子:

  Disch Date  Event ID  Unique ID    Ward ID    Discharging Ward
1 2014-06-14    8208846  A123456       77085     Ward B       
2 2014-05-16    8366113  A123456       77036     Ward A

这是我的输出应该是这样的:

  Disch Date  Event_ID   Unique ID  Ward ID   Discharging Ward
2014-06-14  8208846    A123456   77085      Ward B 

总而言之,我的代码会提供正确的&#34;出院日期&#34;,正确的&#34; Ward ID&#34;但似乎从表中的其他行拉出其余部分。为这个巨大的问题道歉 - 任何帮助都会受到赞赏,或者如果这已被探索过,请指出我正确的方向。

2 个答案:

答案 0 :(得分:0)

您将独立获得每个字段的最大值。如果您想要与最新放电日期对应的行,则需要按降序排列,然后选择= 1的位置。 我们对30天重新承认执行相同类型的查询。 像

这样的东西
WITH 
LastDischargeCTE
AS
(SELECT pat.PAS_ID [K Number], IPS.disch_dttm [Discharge Datetime], (IPS.IP_SPELL_ID) [Spell ID] 
,(IPS.DIS_WARD_ID) [Ward ID]
,DSSU.SU_DESCRIPTION [Discharging Ward]
,ROW_NUMBER () OVER (PARTITION BY pat.PAS_ID ORDER BY IPS.disch_dttm DESC) AS DischargeSequence

FROM Pat_spell
LEFT JOIN PATIENT PAT               WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
LEFT JOIN SPECIALTY SPEC            WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID
WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
AND IPS.DIM_DIS_SPECT_ID = '7195'
AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'
)
SELECT *
FROM LastDischargeCTE 
WHERE DischargeSequence =1

答案 1 :(得分:0)

您需要的简化版本如下所示......

SELECT  [DETAIL INFO, no need to MAX or GROUP BY]
FROM Pat_spell AS IPS
LEFT JOIN PATIENT PAT       WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
LEFT JOIN SPECIALTY SPEC    WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID
INNER JOIN (
    SELECT PatientID, MAX(IPS.disch_dttm) AS DischargeDt
    FROM [AllMyTables]
    WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
    AND IPS.DIM_DIS_SPECT_ID = '7195'
    AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'
) t1 ON PAT.PatientID = t1.PatientID AND IPS.disch_dttm = t1.DischargeDt
ORDER BY pat.PAS_ID

由于INNER SQL每个患者返回1行,因此无需对OUTER SQL进行分组。

如果我有更多的数据可以使用,我可能能够将完整的SQL拼接在一起,但也许这会指向正确的方向。