加入另一个MAX日期表

时间:2015-03-24 15:24:43

标签: sql sql-server join

我有这个sql

SELECT DISTINCT p.dbPatID,
    p.dbpatfirstname,
    p.dbPatLastName,
    s.dbSchTypeCnt AS SchDetailType,
    t.dbSchTypeCnt AS SchTypeType,
    ISNULL(r.dbStatusDesc, 'No Ref') AS dbStatusDesc,
    ISNULL(t.dbSchTypeCode, 'No Ref') AS dbSchTypeCode,
    ISNULL(t.dbSchTypeDesc, 'No Ref') AS dbSchTypeDesc,
    p.dbProgRvw,
    ISNULL(s.dbSchDate, '1899-12-30') AS dbSchDate,
    ISNULL(s.dbSchTypeCnt, '0') AS dbSchTypeCnt,
    p.age,
    ISNULL(rc.dbRecDate, '1899-12-30') AS dbRecDate,
    ISNULL(rc.dbRecType, '-') AS dbRecType,
    ISNULL(rc.dbRecCom, '-') AS dbRecCom,
    a.dbPatApptTime AS LastVisitDate,
    a.dbSchTypeDesc AS LastVisitDesc
FROM Patient p
LEFT JOIN vw_ReferralKPIs r
    ON p.dbpatid = r.dbPatID
        AND r.ClientRef = p.ClientRef
LEFT JOIN SchDetail s
    ON s.dbPatCnt = p.dbPatCnt
        AND s.ClientRef = p.ClientRef
LEFT JOIN SchTypes t
    ON s.dbSchTypeCnt = t.dbSchTypeCnt
        AND t.ClientRef = p.ClientRef
LEFT JOIN Recalls rc
    ON p.dbpatcnt = rc.dbpatcnt
        AND rc.ClientRef = p.ClientRef
LEFT JOIN Appointments a
    ON p.dbpatcnt = a.dbpatcnt
        AND rc.ClientRef = a.ClientRef
        AND dbPFStatus = 1
WHERE (
        r.dbStatusDesc IN ('')
        OR '' = ''
        )
    AND s.dbSchDate <= GetDate()
    AND p.ClientRef = 'EPS'
    AND r.dbStatusDesc != 'Discharged'
    AND r.dbStatusDesc != 'TC Disch'
    AND r.dbStatusDesc != 'Discharge FTA'

我已经添加了约会加入代码,因为我们需要LastVisitDateLastVisitDesc,但我们只希望从Appointments返回具有最大日期(最新日期)的行。

如果我添加一个临时Where子句AND p.dbPatCnt = 9678,它会返回12行(所有人的约会dbPFStatus = 1)。我只想带回具有最新日期的行。

I've found a similar problem/solution但不清楚如何将其应用于我的代码

感谢

继续从该链接开始,我现在......

 SELECT DISTINCT
        p.dbPatID,
        p.dbpatfirstname,
        p.dbPatLastName,
        s.dbSchTypeCnt AS SchDetailType,
        t.dbSchTypeCnt AS SchTypeType,
        ISNULL(r.dbStatusDesc, 'No Ref') AS dbStatusDesc,
        ISNULL(t.dbSchTypeCode, 'No Ref') AS dbSchTypeCode,
        ISNULL(t.dbSchTypeDesc, 'No Ref') AS dbSchTypeDesc,
        p.dbProgRvw,
        ISNULL(s.dbSchDate, '1899-12-30') AS dbSchDate,
        ISNULL(s.dbSchTypeCnt, '0') AS dbSchTypeCnt,
        p.age,
        ISNULL(rc.dbRecDate, '1899-12-30') AS dbRecDate,
        ISNULL(rc.dbRecType, '-') AS dbRecType,
        ISNULL(rc.dbRecCom, '-') AS dbRecCom,
        -- New columns
        ca.LastVisitDate,
        ca.LastVisitDesc
FROM    Patient p
LEFT OUTER JOIN vw_ReferralKPIs r ON p.dbpatid = r.dbPatID
                                     AND r.ClientRef = p.ClientRef
LEFT OUTER JOIN SchDetail s ON s.dbPatCnt = p.dbPatCnt
                               AND s.ClientRef = p.ClientRef
LEFT OUTER JOIN SchTypes t ON s.dbSchTypeCnt = t.dbSchTypeCnt
                              AND t.ClientRef = p.ClientRef
LEFT OUTER JOIN Recalls rc ON p.dbpatcnt = rc.dbpatcnt
                              AND rc.ClientRef = p.ClientRef
LEFT OUTER JOIN ( SELECT    MAX(a.dbPatApptTime) AS LastVisitDate,
                            MAX(a.dbSchTypeDesc) AS LastVisitDesc,
                            a.dbpatcnt
                  FROM      appointments a
                  WHERE     a.dbPFStatus = 1
                            AND a.clientref = 'EPS'
                  GROUP BY  a.dbpatcnt
                ) ca ON ca.dbpatcnt = p.dbpatcnt
WHERE   --(
          --r.dbStatusDesc IN ( '' )
          --OR '' = ''
        --) AND 
        s.dbSchDate <= GETDATE()
        AND p.ClientRef = 'EPS'
        AND r.dbStatusDesc != 'Discharged'
        AND r.dbStatusDesc != 'TC Disch'
        AND r.dbStatusDesc != 'Discharge FTA';

这会带回预期的行数并更正LastVisitDate,但LastVistDesc是错误的。我已经尝试过没有Max并将其包含在Group By中但是这会产生很多重复的行。

感谢,

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT DISTINCT p.dbPatID,
    p.dbpatfirstname,
    p.dbPatLastName,
    s.dbSchTypeCnt AS SchDetailType,
    t.dbSchTypeCnt AS SchTypeType,
    ISNULL(r.dbStatusDesc, 'No Ref') AS dbStatusDesc,
    ISNULL(t.dbSchTypeCode, 'No Ref') AS dbSchTypeCode,
    ISNULL(t.dbSchTypeDesc, 'No Ref') AS dbSchTypeDesc,
    p.dbProgRvw,
    ISNULL(s.dbSchDate, '1899-12-30') AS dbSchDate,
    ISNULL(s.dbSchTypeCnt, '0') AS dbSchTypeCnt,
    p.age,
    ISNULL(rc.dbRecDate, '1899-12-30') AS dbRecDate,
    ISNULL(rc.dbRecType, '-') AS dbRecType,
    ISNULL(rc.dbRecCom, '-') AS dbRecCom,
    a.dbPatApptTime AS LastVisitDate,
    a.dbSchTypeDesc AS LastVisitDesc
FROM Patient p
LEFT JOIN vw_ReferralKPIs r
ON p.dbpatid = r.dbPatID
    AND r.ClientRef = p.ClientRef
LEFT JOIN SchDetail s
ON s.dbPatCnt = p.dbPatCnt
    AND s.ClientRef = p.ClientRef
LEFT JOIN SchTypes t
ON s.dbSchTypeCnt = t.dbSchTypeCnt
    AND t.ClientRef = p.ClientRef
LEFT JOIN Recalls rc
ON p.dbpatcnt = rc.dbpatcnt
    AND rc.ClientRef = p.ClientRef
LEFT JOIN Appointments a
ON p.dbpatcnt = a.dbpatcnt
    AND rc.ClientRef = a.ClientRef
    AND dbPFStatus = 1
WHERE (
    r.dbStatusDesc IN ('')
    OR '' = ''
    )
AND s.dbSchDate <= GetDate()
AND p.ClientRef = 'EPS'
AND r.dbStatusDesc != 'Discharged'
AND r.dbStatusDesc != 'TC Disch'
AND r.dbStatusDesc != 'Discharge FTA'
AND a.dbPatApptTime = (SELECT MAX(a2.dbPatApptTime)
   FROM Appointment a2
   WHERE a2.ClientRef = a.ClientRef
    AND a2.dbPFStatus = 1)

答案 1 :(得分:0)

最好编写一个Table-Value函数,该函数将使用ClientRef和dbpacnt的输入参数返回数据。然后你可以在select语句中包含该函数。

CREATE FUNCTION [dbo].[LastAppointment]
    ( @clientRef INT,
      @dbpatcnt INT
    )
RETURNS TABLE
AS
RETURN
    ( SELECT TOP 1
                a.dbPatApptTime AS LastVisitDate,
                a.dbSchTypeDesc AS LastVisitDesc
      FROM      Appointments a
      WHERE     ClientRef = @clientRef
                AND Dbpatcnt = @dbpatcnt
                AND dbPFStatus = 1
      ORDER BY  a.dbPatApptTime)

GO

然后改变

LEFT JOIN Recalls rc
    ON p.dbpatcnt = rc.dbpatcnt
        AND rc.ClientRef = p.ClientRef
LEFT JOIN Appointments a
    ON p.dbpatcnt = a.dbpatcnt
        AND rc.ClientRef = a.ClientRef
        AND dbPFStatus = 1

LEFT JOIN Recalls rc
    ON p.dbpatcnt = rc.dbpatcnt
        AND rc.ClientRef = p.ClientRef
CROSS APPLY dbo.LastAppointment (rc.ClientRef, p.dbpatcnt)