将查询中的3个视图与特定的Max和Where条件组合在一起

时间:2018-03-21 14:16:56

标签: sql sql-server

我目前正在尝试创建一个来自三个单独视图的查询。在第一个视图中(查看a。名为view_ods_assessment)我想在此视图中提取所有列,但是,只想拉出特定的stdAssessID并且只在此视图中为每个PatientID拉出最大值(或最近的AssessmentDate) 。 在此查询中,我还想从第二个视图中仅提取Description列(视图b。称为view_ods_std_assessment),只要视图中的stdAssessID =视图b中的stdAssessID即可。 从第三个视图(视图c。称为view_ods_daily_census)我想引入PatientFirstName,PatientLastName,PatientMiddleName,FacilityName和PayerName,只要在视图“a”中显示PatientID。 =来自视图“c。”的ClientID。

通过输入以下查询,我能够获得我的信息,但是,目前正在提取所有评估,而不是仅提取每个PatientID的最新评估。我在尝试创建一个将运行的查询时遇到一些问题,并为每个PatientID拉出最大的AssessmentDate。有人可以帮忙解决这个问题吗?

SELECT a.AssessmentID, a.stdAssessID, a.PatientID, a.AssessmentDate, a.AssessmentStatus, a.AssessmentTypeKey, a.CommunicationScale, a.CognativePerformanceScale, a.DepressionRatingScale, a.PainScale, 
a.ActivityDailyLivingScore, a.MedicareCMI, a.MedicareRUG, a.MedicareNonTherapyCMI, a.MedicareNonTherapyRUG, a.StateCMI, a.StateRUG, a.StateAlternateCMI, a.StateAlternateRUG, a.Score3, a.CreatedDate, a.RevisionDate, a.Deleted, a.IncorrectAssessmentID, a.FacilityID, a.CreatedBy, a.CompletedDate, a.LockedDate, a.RevisionBy, a.DeletedBy, a.DeletedDate, a.MDSAcceptedDate, a.BatchID, b.Description, c.PatientFirstName, c.PatientLastName, c.PatientMiddleName, c.FacilityName, c.PayerName
FROM view_ods_assessment AS a 
LEFT JOIN view_ods_std_assessment AS b ON a.stdAssessID = b.StdAssessID 
LEFT JOIN view_ods_daily_census AS c ON a.PatientID = c.ClientID
WHERE a.stdAssessID IN ('1', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13')

1 个答案:

答案 0 :(得分:0)

如果您想沿着ROW_NUMBER()路线走,那么您可能希望使用公用表表达式来预先计算它,然后将其用作过滤器。所以这样的事情可能有用:

WITH cte AS (
    SELECT 
        a.AssessmentID, 
        a.stdAssessID, 
        a.PatientID, 
        a.AssessmentDate, 
        a.AssessmentStatus, 
        a.AssessmentTypeKey, 
        a.CommunicationScale, 
        a.CognativePerformanceScale, 
        a.DepressionRatingScale, 
        a.PainScale, 
        a.ActivityDailyLivingScore, 
        a.MedicareCMI, 
        a.MedicareRUG,
        a.MedicareNonTherapyCMI, 
        a.MedicareNonTherapyRUG, 
        a.StateCMI, 
        a.StateRUG, 
        a.StateAlternateCMI, 
        a.StateAlternateRUG, 
        a.Score3, 
        a.CreatedDate, 
        a.RevisionDate, 
        a.Deleted, 
        a.IncorrectAssessmentID, 
        a.FacilityID, 
        a.CreatedBy, 
        a.CompletedDate, 
        a.LockedDate, 
        a.RevisionBy, 
        a.DeletedBy, 
        a.DeletedDate, 
        a.MDSAcceptedDate, 
        a.BatchID, 
        b.[Description], 
        c.PatientFirstName, 
        c.PatientLastName, 
        c.PatientMiddleName, 
        c.FacilityName, 
        c.PayerName,
        ROW_NUMBER() OVER (PARTITION BY a.PatientID ORDER BY a.AssessmentDate DESC) AS patient_row_id
    FROM 
        view_ods_assessment AS a 
        LEFT JOIN view_ods_std_assessment AS b ON a.stdAssessID = b.StdAssessID 
        LEFT JOIN view_ods_daily_census AS c ON a.PatientID = c.ClientID
    WHERE 
        a.stdAssessID IN ('1', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'))
SELECT
    * --too lazy to list all those columns again
FROM
    cte
WHERE
    patient_row_id = 1;

关键的变化是ROW_NUMBER() OVER (PARTITION BY a.PatientID ORDER BY a.AssessmentDate DESC) AS patient_row_id行,基本上说,“给我一个从1开始并继续增加1的数字,按患者ID分配这个数字,所以每个都重新开始1唯一的患者ID,并按照反向日期顺序在评估日期订购编号“。

要获得每位患者的最新评估,您只需过滤掉此数字等于1的行。