0

何よりもまず、SQLはサーバーによって動的に処理されるため、私のWHERE句の一部の項目は奇妙に見えるかもしれません。これらは問題ではないため、無視してください。

私のクライアントの要求によると、彼らはUNION私の更新レポートに他の2つの条件(/患者の訪問のない患者/)と(/予約のない患者/)をする必要がありました。これら2つのサブセットの患者を最終更新クエリに追加するのに助けが必要です。現在の状態では、患者を追加するだけで#Tempあり、追加の患者を組み込む必要があります。

どんな助けでも大歓迎です。

私の現在のSQLアップデート-

DECLARE @Inactive INT
DECLARE @Active INT
DECLARE @PatientProfileId INT

SELECT
    @Inactive = MedlistsId
FROM
    Medlists
WHERE
    TableName = 'PatientProfileStatus'
    AND Code = 'I'

SELECT
    @Active = MedlistsId
FROM
    Medlists
WHERE
    TableName = 'PatientProfileStatus'
    AND Code = 'A'

CREATE TABLE #Temp
    (
      PatientName VARCHAR(120) ,
      PatientProfileId INT ,
      RecentId INT ,
      Recent DATETIME
    )

INSERT  INTO #Temp
        SELECT
            dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) AS Name ,
            pp.PatientProfileId ,
            MAX(pv.PatientVisitId) AS RecentId ,
            MAX(pv.Visit) AS Recent
        FROM
            PatientVisit pv
            INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId
                                            AND pp.PatientStatusMId = @Active
        WHERE
            pp.PatientProfileId IN ( SELECT
                                        a.OwnerId
                                     FROM
                                        Appointments a
                                        INNER JOIN PatientProfile pp ON a.OwnerId = pp.PatientProfileId
                                                                        AND a.ApptKind = 1
                                                                        AND pp.PatientStatusMId = @Active
                                     GROUP BY
                                        a.OwnerId ,
                                        a.ApptKind
                                     HAVING
                                        MAX(a.ApptStart) < '07/30/2005' )
        GROUP BY
            dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) ,
            pp.PatientProfileId
        HAVING
            MAX(pv.Visit) < '07/30/2005' 

/*Patients without a Appointment*/

IF 1 = 1 
    INSERT  INTO #Temp
            SELECT
                dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) AS Name ,
                pp.PatientProfileId ,
                NULL AS RecentId ,
                NULL AS Recent
            FROM
                PatientProfile pp
                LEFT JOIN ( SELECT * FROM Medlists WHERE TableName = 'PatientProfileStatus' ) ml1 ON pp.PatientStatusMId = ml1.MedlistsId
                LEFT JOIN Appointments a ON a.Ownerid = pp.PatientProfileId
                                            AND a.ApptKind = 1
                LEFT JOIN PatientVisit pv ON a.PatientVisitId = pv.PatientVisitId
            WHERE
                ml1.Code = 'A'
                AND a.ownerid IS NULL
                AND --Filter on Age
                (
                  ((
                     '-1' = '-1'
                     AND '40' = '125'
                   )
                  OR ( CAST(( DATEDIFF(DAY , pp.Birthdate , GETDATE()) / 365.25 ) AS INT) BETWEEN ( '-1' ) AND ( '40' ) ))
                )

/*Patients without a Patient Visit*/

IF 0 = 1 
    INSERT  INTO #Temp
            SELECT
                dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) AS Name ,
                pp.PatientProfileId ,
                NULL AS RecentId ,
                NULL AS Recent
            FROM
                PatientProfile pp
                LEFT JOIN PatientVisit pv ON pv.PatientProfileid = pp.PatientProfileid
                LEFT JOIN ( SELECT * FROM Medlists WHERE TableName = 'PatientProfileStatus' ) ml1 ON pp.PatientStatusMId = ml1.MedlistsId
            WHERE
                ml1.Code = 'A'
                AND pv.patientprofileid IS NULL
                AND --Filter on Age
                (
                  ((
                     '-1' = '-1'
                     AND '40' = '125'
                   )
                  OR ( CAST(( DATEDIFF(DAY , pp.Birthdate , GETDATE()) / 365.25 ) AS INT) BETWEEN ( '-1' ) AND ( '40' ) ))
                )   

DECLARE curPatient CURSOR FORWARD_ONLY READ_ONLY LOCAL
FOR
    SELECT
        t.PatientProfileId
    FROM
        #Temp t
        JOIN PatientProfile pp ON t.PatientProfileId = pp.PatientProfileId
        JOIN PatientVisit pv ON pp.PatientProfileId = pv.PatientProfileId
                                AND pv.PatientVisitId = t.RecentId
    WHERE
        --Filter on Age
        (
          ((
             '-1' = '-1'
             AND '40' = '125'
           )
          OR ( CAST(( DATEDIFF(DAY , pp.Birthdate , GETDATE()) / 365.25 ) AS INT) BETWEEN ( '-1' ) AND ( '40' ) ))
        )

OPEN curPatient
FETCH NEXT FROM curPatient INTO @PatientProfileId

WHILE @@FETCH_STATUS = 0 
    BEGIN  

        UPDATE
            PatientProfile
        SET 
            PatientStatusMId = @Inactive ,
            pstatus = 'I'
        FROM
            PatientProfile P
            INNER JOIN #Temp t ON t.PatientProfileID = P.PatientProfileID
        WHERE
            p.PatientProfileId = @PatientProfileId 


        FETCH NEXT FROM curPatient INTO @PatientProfileId 
    END 

CLOSE curPatient
DEALLOCATE curPatient   

DROP TABLE #Temp
4

1 に答える 1

0

ご予約の患者様限定となります

LEFT JOIN Appointments a 
       ON a.Ownerid = pp.PatientProfileId
      AND a.ApptKind = 1

これは、訪問のある患者に限定されます

LEFT JOIN PatientVisit pv 
       ON pv.PatientProfileid = pp.PatientProfileid

試す

LEFT OUTER JOIN Appointments a 
           ON a.Ownerid = pp.PatientProfileId
          AND a.ApptKind = 1
Where a.Ownerid is null


LEFT OUTER JOIN PatientVisit pv 
       ON pv.PatientProfileid = pp.PatientProfileid
Where pv.PatientProfileid is null
于 2012-12-28T17:02:35.750 に答える